Link to home
Start Free TrialLog in
Avatar of jimboVB
jimboVB

asked on

How to connect to LDAP in JSP?

I have a problem. What i want to do now is use jsp to connect to a LDAP database to retrieve the datas. i have a form page where the user logs in and it passes through LDAP to check if the userid exists. If it does, it checks for the password to check if it is right. When both fields(userid and password) are correct, the user will be directed to a page where the user can change his/her password there, and the new password will be updated to the LDAP database.

I've been searching the web on how to get this done. And eventually found out that many steps needed to be done. I just want a simple connection to LDAP on the internet and update the password, as easy as that. Thanks.

Jimbo
Avatar of thanassis
thanassis

First of all I think you must write java classes to do the job and then use this in jsp pages.

Have a look there:

http://developer.novell.com/ndk/doc/samplecode/jldap_sample/index.htm

I think it is exactlly what you want
Avatar of jimboVB

ASKER

Hi, thanks for the quick response. I'll explain my problem clearer.

I want to

1. Connect to LDAP on the internet
2. User logs in a form with 'userid' and 'password'
3. Retrieve user's record from LDAP
3. Check current password if it's correct and direct user to a form page where they can enter new password.
4. Update records into LDAP with new password entered by user in the change password field.

Hope to have a clearer understanding of how this is done cuz i'm quite new to all these, thanks.
All this steps is a lot of work to explain here.
So my suggestion is to start with the connetion.

1. you want a JDBC driver that can communicate with an LDAP server.

You can download one from here:

http://www.octetstring.com/Support/jdbcldapdriver/GettingStarted.html#Obtaining

Read the documents that comes with the downloaded files add try to install the driver.
If you do all this correct, then start thinking for step 2.

good luck :)
Avatar of jimboVB

ASKER

where do i put my 'jdbcLdap.jar ' into?
when i paste this line into jsp
Class.forName("com.octetstring.jdbcLdap.sql.JdbcLdapDriver");
i get an error.
Avatar of jimboVB

ASKER

where do i put my 'jdbcLdap.jar ' into?
when i paste this line into jsp
Class.forName("com.octetstring.jdbcLdap.sql.JdbcLdapDriver");
i get an error.
No, No, you must install the driver in your web Server (Tomcat I suppose)
Avatar of jimboVB

ASKER

ermm... my webserver is iPlanet, so ermm.. where should i put in..?
Sorry I can't help to this. (I don't use iPlanet)
But maybe somewhere there is a lib directory with jar files. Try put it there.
Avatar of jimboVB

ASKER

or is it maybe because i dont have a jdbc driver?
sorry to ask so much question. Hope you can help me on this.
I suppose that file jdbcLdap.jar is a jdbc driver.
And your web server doesn't have it installed by default.

Better ask in the web servers area someone who knows iPlanet)to solve your instalation problem.
Avatar of jimboVB

ASKER

Does anyone know how to work this out? thanks a lot.
you have 2 ways to connect to LDAP, via JDBC (old way) or  JNDI (new way).

the the jdbc part, I think you need to put the .jar file in
iPlanet/ias6/ias/lib, or it still doesn't work, put it under youwebapp/WEB-INF/lib.

if you prefer JNDI to access LDAP, which I prefer:
you need to create InitialContext as:

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://ldap.wiz.com:389");
env.put(Context.SECURITY_PRINCIPAL, "joeuser");
env.put(Context.SECURITY_CREDENTIALS, "joepassword");
Context ctx = new InitialContext(env);

for the above code to work, you need to import java.util and javax.naming package.

for detail of how to use jndi, read the tutorial:
http://java.sun.com/products/jndi/tutorial/

let me know if you have futher enquires.

Avatar of jimboVB

ASKER

kennethxu:
ermm.. so i just have to paste the above codes you provided into my jsp page and that's it?

<%@page language="java" import="java.sql.* , java.util.*, javax.naming.* " %>

<%
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://ldap.wiz.com:389");
env.put(Context.SECURITY_PRINCIPAL, "joeuser");
env.put(Context.SECURITY_CREDENTIALS, "joepassword");
Context ctx = new InitialContext(env);

%>
Avatar of jimboVB

ASKER

one problem, when i import  javax.naming.*, i get an error.
that means you application server and/or jdk doesn't have jndi library. you need to download jndi 1.2.1 class library and ldap service provider from:
http://java.sun.com/products/jndi/#DOWNLOAD12
also, you must change to your ldap server name and user/password used to connect to ldap server.
Avatar of jimboVB

ASKER

where do i extract the jndi library into?
try to put them under youwebapp/WEB-INF/lib.
Avatar of jimboVB

ASKER

oh no.. i've pasted the jar files everywhere i can think of, it still never work. this is driving me crazy.. is there any easier tutorials on how to get these done..?
are you still getting javax.naming package not found problem? or you are getting other errors, can you post the detailed error message with stacktrace?
Avatar of jimboVB

ASKER

sorry for the late reply.

kennethxu:
i got the javax.naming package solved, and the codes you provided working, but there is a part not working. It's the last line,

Context ctx = new InitialContext(env);

i get a server error. But dont know why. i'm using iPlanet, when i get errors, it always show the same error. When i comment the above code, there's no error, what should i do? thanks

Jimbo
can you post the error message? did you changed the ldap server name in the code to your real ldap server?
Avatar of jimboVB

ASKER

here's my set of codes

<%@page language="java" import="java.sql.* , java.util.* ,javax.naming.*" %>
<%
//Class.forName("com.octetstring.jdbcLdap.sql.JdbcLdapDriver");
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://woodlands.onenorth.org.sg:10390");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "username");
env.put(Context.SECURITY_CREDENTIALS, "password");
Context ctx = new InitialContext(env);

the error i have metioned to you before. here it is.

Server Error
This server has encountered an internal error which prevents it from fulfilling your request. The most likely cause is a misconfiguration. Please ask the administrator to look for messages in the server's error log.

it always shows this error when there is problems with my codes.
can you have a look at the server log? it's hard to tell if we don't know the real error.
Avatar of jimboVB

ASKER

ermm... sorry, i'm not sure how to read the server log from. do you know where i can find the server log?
Avatar of jimboVB

ASKER

i think i found it, here's the error

[10/Mar/2003:11:26:09] failure (  604): Internal error: servlet service function had thrown ServletException (uri=/banner/changePassword/editpwd.jsp): javax.servlet.ServletException: Cannot instantiate class: com.sun.jndi.ldap.LdapCtxFactory, stack: javax.servlet.ServletException: Cannot instantiate class: com.sun.jndi.ldap.LdapCtxFactory
it may be under <ips-install>/server4/ https-<hostname>/logs
it still cannot find the driver jar :(
did you downloaded the ldap service provider jar file, you should have 2 jar files downloaded.
Avatar of jimboVB

ASKER

oh no.. i've tried to paste the jar files anywhere i could think of. would this be a problem because i've been pasting the same file to many different folders? if it's not a problem doing this, i'll try to paste again and again.
if not please download Ldap Service Provider 1.2.4 from
http://java.sun.com/products/jndi/#DOWNLOAD12
Avatar of jimboVB

ASKER

yeah... total i have 3 jar files that they want me to paste into my lib (jndi.jar, ldap.jar and providerutil.jar)
Avatar of jimboVB

ASKER

oh no.. i think i missed that, let me try again and get back to you. thanks a lot. just wait a moment.. ^^.
hmm, BTW, are you sure your ldap server is running on port 10390?

There was a big shopping center in construction near woodlands mrt station when i left singapore, how is it :)
jimbo, it's late here, I'll talk to you tommorrow.
Avatar of jimboVB

ASKER

wow... you've stayed in singapore before? i'm not sure about woodlands, i'm staying in the east.. ^^.
sorry to disturb. hope you can help me again some other times, thanks a lot for the help.

Jimbo
Avatar of jimboVB

ASKER

i still get the same error. Is it maybe that my ldap server's address and port is wrong that have caused this error?
>> i still get the same error. Is it maybe that my ldap server's address and port is wrong that have caused this error?

that's what I guess!

can you just write a simple standalone java program to try jndi out?

you need to include every jar file in the classpath, compile and execute the java program. I'm trying to isolate the server problem and jndi problem.

>> sorry to disturb. hope you can help me again some other times, thanks a lot for the help.
no problem, I'm do my best to help :)
Avatar of jimboVB

ASKER

hi there again.
yesterday i've uninstalled my iPlanet and reinstalled all over again. I've added the jar files into my classpath, and now it works! =)

thanks for so much help kennethxu, you've answered most of my questions, really appreciate it.

now i have one more question. Do you know where i can find tutorials or examples on using LDAP in JSP? i've search in most websites, they only have LDAP in JAVA. I know both are quite the same but hope there is LDAP in JSP tutorials.
ASKER CERTIFIED SOLUTION
Avatar of kennethxu
kennethxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jimboVB

ASKER

hi there again...

i can be able to connect to my own LDAP local server and retrieve the information using the codes you gave me above. But when i use the codes to connect to other LDAP server, i cannot retrieve any information.

example when i perform a search in my LDAP server, i can retrieve the user data, but when i perform a search on other LDAP server, no record was found. Does it mean that the server also must include the jar files in their classpath or something?
Avatar of jimboVB

ASKER

kennethxu:

Hi!... thanks for helping out. You deserve more pts, but sorry, i've only increased abit, due to lack of pts..  =(


Jimbo
Hi Jimbo,

You are very welcome and thanks for your points. I'm sorry that I was tied up with something so didn't reply you yesterday. Is all your problem solved? I used LDAP a year ago and I don't have an LDAP server to test now, so my help was very limited.

Happy coding,
Kenneth