• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4257
  • Last Modified:

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
0
jimboVB
Asked:
jimboVB
  • 22
  • 16
  • 5
1 Solution
 
thanassisCommented:
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
0
 
jimboVBAuthor Commented:
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.
0
 
thanassisCommented:
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 :)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jimboVBAuthor Commented:
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.
0
 
jimboVBAuthor Commented:
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.
0
 
thanassisCommented:
No, No, you must install the driver in your web Server (Tomcat I suppose)
0
 
jimboVBAuthor Commented:
ermm... my webserver is iPlanet, so ermm.. where should i put in..?
0
 
thanassisCommented:
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.
0
 
jimboVBAuthor Commented:
or is it maybe because i dont have a jdbc driver?
sorry to ask so much question. Hope you can help me on this.
0
 
thanassisCommented:
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.
0
 
jimboVBAuthor Commented:
Does anyone know how to work this out? thanks a lot.
0
 
kennethxuCommented:
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.

0
 
jimboVBAuthor Commented:
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);

%>
0
 
jimboVBAuthor Commented:
one problem, when i import  javax.naming.*, i get an error.
0
 
kennethxuCommented:
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
0
 
kennethxuCommented:
also, you must change to your ldap server name and user/password used to connect to ldap server.
0
 
jimboVBAuthor Commented:
where do i extract the jndi library into?
0
 
kennethxuCommented:
try to put them under youwebapp/WEB-INF/lib.
0
 
jimboVBAuthor Commented:
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..?
0
 
kennethxuCommented:
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?
0
 
jimboVBAuthor Commented:
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
0
 
kennethxuCommented:
can you post the error message? did you changed the ldap server name in the code to your real ldap server?
0
 
jimboVBAuthor Commented:
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.
0
 
kennethxuCommented:
can you have a look at the server log? it's hard to tell if we don't know the real error.
0
 
jimboVBAuthor Commented:
ermm... sorry, i'm not sure how to read the server log from. do you know where i can find the server log?
0
 
jimboVBAuthor Commented:
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
0
 
kennethxuCommented:
it may be under <ips-install>/server4/ https-<hostname>/logs
0
 
kennethxuCommented:
it still cannot find the driver jar :(
0
 
kennethxuCommented:
did you downloaded the ldap service provider jar file, you should have 2 jar files downloaded.
0
 
jimboVBAuthor Commented:
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.
0
 
kennethxuCommented:
if not please download Ldap Service Provider 1.2.4 from
http://java.sun.com/products/jndi/#DOWNLOAD12
0
 
jimboVBAuthor Commented:
yeah... total i have 3 jar files that they want me to paste into my lib (jndi.jar, ldap.jar and providerutil.jar)
0
 
jimboVBAuthor Commented:
oh no.. i think i missed that, let me try again and get back to you. thanks a lot. just wait a moment.. ^^.
0
 
kennethxuCommented:
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 :)
0
 
kennethxuCommented:
jimbo, it's late here, I'll talk to you tommorrow.
0
 
jimboVBAuthor Commented:
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
0
 
jimboVBAuthor Commented:
i still get the same error. Is it maybe that my ldap server's address and port is wrong that have caused this error?
0
 
kennethxuCommented:
>> 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 :)
0
 
jimboVBAuthor Commented:
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.
0
 
kennethxuCommented:
glad to know your problem is solved.

I think those java tutorials also apply to jsp pages. you know you can put java code in <% %> scriptlet, and you can define method in <%! %>.

You might also look into write helper java class/bean to be used in jsp page.
0
 
jimboVBAuthor Commented:
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?
0
 
jimboVBAuthor Commented:
kennethxu:

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


Jimbo
0
 
kennethxuCommented:
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 22
  • 16
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now