Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Java
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 04-08-2005, 11:35 AM   #1 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
how the servlet will integrate the LDAP code

Hello all,
I have a code that adds the entries to the LDAP directory server and this code is in JAVA
But this code does compile but when i run it it gives Runtime error as
::::: Invalid credentials::::::

The server is running when the code is run. for this do i need to specify certain schema in the .CONF file or anything else
How to integate this code into the servlet????

The server I am using is OpenLDAP for Windows and Netscape directory SDK for JAVA

plz tell me about some another server available for LDAP support(free version) with JAVA (For Windows)
Plz tell me about the server and other information about LDAP Directory server so that i could find support for it easily

The code is as below::::::::::::::::::





Code:
import netscape.ldap.*; import java.util.*; public class Add { public static void main( String[] args ) { /* Specify the DN we're adding */ //String nickname = rock.nickname; String dn = "uid=ac" ; /* Specify the attributes of the entry */ String objectclass_values[] = { "top", "person", "organizationalPerson", "inetOrgPerson" }; LDAPAttributeSet attrs = new LDAPAttributeSet(); LDAPAttribute attr = new LDAPAttribute( "objectclass" ); for( int i = 0; i < objectclass_values.length; i++ ) { attr.addValue( objectclass_values[i] ); } attrs.add( attr ); attrs.add( new LDAPAttribute( "uid", "wbjensen" ) ); /* Create an entry with this DN and these attributes */ LDAPEntry myEntry = new LDAPEntry( dn, attrs ); LDAPConnection ld = null; int status = -1; try { ld = new LDAPConnection(); /* Connect to server */ String MY_HOST = "localhost"; int MY_PORT = 389; ld.connect( MY_HOST, MY_PORT ); /* Authenticate to the server as directory manager */ String MGR_DN = "dc=monarch,dc=com"; String MGR_PW = "ali"; ld.authenticate( MGR_DN, MGR_PW ); System.out.println("Entry established"); /* Now add the entry to the directory */ ld.add( myEntry ); System.out.println( "Entry added" ); System.out.println("Entry established1111"); } catch( LDAPException e ) { if ( e.getLDAPResultCode() == LDAPException.ENTRY_ALREADY_EXISTS ) System.out.println( "Error: Entry already present" ); else System.out.println( "Error: " + e.toString()+"AA che error" ); } /* Done, so disconnect */ if ( (ld != null) && ld.isConnected() ) { try { ld.disconnect(); } catch ( LDAPException e ) { System.out.println( "Error: " + e.toString() ); } } System.exit(status); } }
Regards
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-08-2005, 06:05 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
Wow, I didn't think anyone other than legacy maintainers used the Netscape LDAP packages, I'm not even sure if they're still maintained . . . I think there was a bug in the last version I used. If it's possible, you really should switch over to JNDI, as it's part of the Java SDK, so it's more widely used and will probably be supported as long as Java is used.

As for another LDAP server, you might be able to find trial versions, but for an absolutely free version OpenLDAP is pretty much it.

Now, I don't think you're actually connecting as a user, although I'm a bit rusty so I could be wrong. Is "dc=monarch,dc=com" the actual DN of a user in the directory? Further, does this user have the permissions to perform the operations you have requested? The error message pretty clearly indicated that you've failed to authenticate, so you need to concentrate on that.

Man, it's been a while since I worked with LDAP. I love that protocol . . .
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-08-2005, 09:44 PM   #3 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
Re

Hello
Thanks for the reply
Now i have dc=monarch,dc=com as the authId so it could perform all the operations and moreover i wanted to ask that if i wanted to add the entry the DN should have the BaseDN as the suffix is it so??

And if u know how to add the entries using jndi then plz let me know

Thanks
Regards
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-09-2005, 02:34 AM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
Sun has a tutorial on using JDNI. Here is another tutorial you can look through, as it might be a little easier to follow.

Basically, I don't think you're connecting as a priviledged user. "dc=monarch,dc=com" is the suffix of the directory, or the top, but it's not a root DN. You need to create a root DN underneath this suffix in OpenLDAP and make sure it has administrative privledges.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-09-2005, 09:17 PM   #5 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
Re ;;

Hello
thanks for your reply. I had modified the authid and now i can authenticate witht the ldap server but there is an Operation error

That occurs while the entries are added to the directory
What must be the cause looking at the code i had written

anything else or schema needs to be changed to add the entries

Plz reply
Regards

Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-09-2005, 11:30 PM   #6 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
re:::

Hello

I am able to connect to the ldap server by calling the Add.java class into any .java class and it connects successfully but the entry is not added
The code for the Add.java is being checked standalone also only it does not run with the servlet.

But when i call the same code within the servlet it does not show the result
The log file within the container od the servlet shows

LDAPException not found in doPost() method

Here is my servlet code

Code:
package com.chat.java; import netscape.ldap.*; import netscape.ldap.util.*; import netscape.ldap.controls.*; import java.io.*; import java.sql.*; import javax.servlet.ServletException; import javax.servlet.http.*; import javax.swing.*; import netscape.ldap.LDAPConnection; import com.chat.ChatRoomList; import com.chat.ChatRoom; import com.chat.Add; public class LoginServlet extends HttpServlet { LDAPConnection ld ; boolean flag = false; String s1,s2; int i = 1800; public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doPost(req,res); } public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse) throws ServletException, IOException { String s = "jdbc:odbc:chat"; contextPath = httpservletrequest.getContextPath(); s1 = httpservletrequest.getParameter("nickname"); s2 = httpservletrequest.getParameter("pwd"); s1 = s1.trim().toLowerCase(); if(s2.length() > 0) s2 = s2.trim().toLowerCase(); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection connection = DriverManager.getConnection(s); Statement statement = connection.createStatement(); for(ResultSet resultset = statement.executeQuery("select * from Login"); resultset.next();) { String s3 = resultset.getString(1); String s4 = resultset.getString(2); if(s1.equals(s3) && s2.equals(s4)) { flag = true; } } }//try ends here catch(SQLException sqlexception) { System.out.println("Error...." + sqlexception); } catch(ClassNotFoundException classnotfoundexception) { System.out.println("Error...." + classnotfoundexception); } if(flag=true) { try { ChatRoomList chatroomlist = (ChatRoomList)getServletContext().getAttribute("chatroomlist"); boolean flag1 = chatroomlist.chatterExists(s1); // THIS IS THE PLACE WHICH DOES NOT HAPPEN // THE OBJECT IS NOT CREATED // EXECUTION AFTER IT STOPS Add add = new Add(); System.out.println("After add"); if(flag1) { JOptionPane.showMessageDialog(null,"U have already Login......","Information",JOptionPane.INFORMATION_MESSAGE); } else { HttpSession httpsession = httpservletrequest.getSession(true); int i = 1800; String s2 = getServletContext().getInitParameter("sessionTimeout"); if(s2 != null) try { i = Integer.parseInt(s2); i *= 60; } catch(NumberFormatException numberformatexception) { } httpsession.setMaxInactiveInterval(i); httpsession.setAttribute("nickname", s1); } }//try ends catch(Exception exception) { System.out.println("Exception thrown in LoginServlet: " + exception.getMessage()); exception.printStackTrace(); } }//if ends here else { JOptionPane.showMessageDialog(null,"Server is saying that Invalid User","Information",JOptionPane.INFORMATION_MESSAGE); } }//do post ends here private final void _mththis() { contextPath = ""; } public LoginServlet() { _mththis(); } private String contextPath; ObjectOutputStream out=null; }

Please help

Regards
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-10-2005, 07:32 AM   #7 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
The problem, I think, is still authentication. With LDAP, authentication and connection are really two different steps. When you connect to LDAP, and pass it a set of credentials, you could fail and be allowed in as an anonymous user. I can't remember if the Netscape LDAP packages threw an exception for a failed login or not. But, because you're not authenticated as a user with sufficent rights to actually modify anything, when you try to add an entry it will fail.

As for the Servlet, the problem obviously lies in Add(). We'd best debug that first before you add layers on top of it.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-10-2005, 10:03 AM   #8 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
Hello
thanks for the reply. I have tested the code and the Add.java works quite fine it is when it enters the servlet the problem starts.
/But if u know about the jndi concept then please let me know

Thanks
Regards
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-10-2005, 10:20 AM   #9 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
Quote:
but the entry is not added
I was under the impression that you wanted it to add, so there was still a problem.

Anyways, you never declared a constructor for Add, that might be a problem as well.

As for JNDI, see the links I provided earlier.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-11-2005, 12:23 PM   #10 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
re:::

Hello
I have ran the whole code to add the entries standalone and the entries are added

But when i did it using servlet the servlet does not execute any line of the code even no print statement I could see on the console of the web server
I suppose the whole code is being bypassed coz theres no error in the log file

Plz help
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-12-2005, 06:25 AM   #11 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
Well, if you're using the Add that you posted up at the top, it won't do anything because you never defined a constructor.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-12-2005, 11:15 AM   #12 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
Re

Hello
I have made the necessary changes that u had told me but the servlet is not being able to retrieve the LDAP classes or I could say it is not able to recognize the package

import netscape.ldap.*;

this is not found by the servlet

So do I need to place it in the container all the classes that this package includes

Please reply

Regards
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-12-2005, 11:26 AM   #13 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
Yes, the container won't be using your classpath, so you need to put the Netscape LDAP packages in WEB-INF/classes if they're just .class files, or WEB-INF/lib if they're jars.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-12-2005, 12:45 PM   #14 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
Hello
thanks for the reply. actually the jars are being placed in the lib itself then also the servlet is unable to find the package netscape.ldap.* and not a single class of the LDAP add functionality is able to create an object

What might be the case now

Thanks
Regards
Jignesh
__________________
j.gohel is offline   Reply With Quote
Old 04-12-2005, 12:48 PM   #15 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
What do you mean by "the lib itself"?
__________________
GitS
Belisarius is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
{perl} HTTP Error Code Response gty All Other Coding Languages 2 09-08-2004 11:09 AM
Cisco Code breaking sde Code Newbie News 0 05-21-2004 07:10 AM
Microsoft probes Windows code leak redhead Code Newbie News 0 02-13-2004 12:41 AM
VBA: My Outlook VBA rule code does't work :( gicio Visual Basic 6 1 11-25-2003 05:16 AM
An Introduction to XHTML/CSS Rie HTML / CSS 0 03-07-2003 06:50 PM


All times are GMT -8. The time now is 06:36 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle