?
Solved

How to connect to LDAP in JSP?

Posted on 2003-03-04
43
Medium Priority
?
4,218 Views
Last Modified: 2011-08-18
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
Comment
Question by:jimboVB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 22
  • 16
  • 5
43 Comments
 
LVL 4

Expert Comment

by:thanassis
ID: 8070193
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
 
LVL 1

Author Comment

by:jimboVB
ID: 8070238
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
 
LVL 4

Expert Comment

by:thanassis
ID: 8070312
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:jimboVB
ID: 8070427
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
 
LVL 1

Author Comment

by:jimboVB
ID: 8070442
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
 
LVL 4

Expert Comment

by:thanassis
ID: 8070448
No, No, you must install the driver in your web Server (Tomcat I suppose)
0
 
LVL 1

Author Comment

by:jimboVB
ID: 8070468
ermm... my webserver is iPlanet, so ermm.. where should i put in..?
0
 
LVL 4

Expert Comment

by:thanassis
ID: 8070507
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
 
LVL 1

Author Comment

by:jimboVB
ID: 8070547
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
 
LVL 4

Expert Comment

by:thanassis
ID: 8070590
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
 
LVL 1

Author Comment

by:jimboVB
ID: 8071804
Does anyone know how to work this out? thanks a lot.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8072670
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
 
LVL 1

Author Comment

by:jimboVB
ID: 8076588
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
 
LVL 1

Author Comment

by:jimboVB
ID: 8076662
one problem, when i import  javax.naming.*, i get an error.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8076812
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8076814
also, you must change to your ldap server name and user/password used to connect to ldap server.
0
 
LVL 1

Author Comment

by:jimboVB
ID: 8077055
where do i extract the jndi library into?
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8077076
try to put them under youwebapp/WEB-INF/lib.
0
 
LVL 1

Author Comment

by:jimboVB
ID: 8078456
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8081177
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
 
LVL 1

Author Comment

by:jimboVB
ID: 8100136
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8100453
can you post the error message? did you changed the ldap server name in the code to your real ldap server?
0
 
LVL 1

Author Comment

by:jimboVB
ID: 8100470
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8100493
can you have a look at the server log? it's hard to tell if we don't know the real error.
0
 
LVL 1

Author Comment

by:jimboVB
ID: 8100506
ermm... sorry, i'm not sure how to read the server log from. do you know where i can find the server log?
0
 
LVL 1

Author Comment

by:jimboVB
ID: 8100524
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8100528
it may be under <ips-install>/server4/ https-<hostname>/logs
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8100539
it still cannot find the driver jar :(
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8100543
did you downloaded the ldap service provider jar file, you should have 2 jar files downloaded.
0
 
LVL 1

Author Comment

by:jimboVB
ID: 8100548
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8100551
if not please download Ldap Service Provider 1.2.4 from
http://java.sun.com/products/jndi/#DOWNLOAD12
0
 
LVL 1

Author Comment

by:jimboVB
ID: 8100555
yeah... total i have 3 jar files that they want me to paste into my lib (jndi.jar, ldap.jar and providerutil.jar)
0
 
LVL 1

Author Comment

by:jimboVB
ID: 8100560
oh no.. i think i missed that, let me try again and get back to you. thanks a lot. just wait a moment.. ^^.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8100580
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8100583
jimbo, it's late here, I'll talk to you tommorrow.
0
 
LVL 1

Author Comment

by:jimboVB
ID: 8100621
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
 
LVL 1

Author Comment

by:jimboVB
ID: 8100853
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8104195
>> 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
 
LVL 1

Author Comment

by:jimboVB
ID: 8107721
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
 
LVL 14

Accepted Solution

by:
kennethxu earned 600 total points
ID: 8107964
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
 
LVL 1

Author Comment

by:jimboVB
ID: 8116327
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
 
LVL 1

Author Comment

by:jimboVB
ID: 8118338
kennethxu:

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


Jimbo
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8120226
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question