aspose file tools*
The moose likes Servlets and the fly likes Calling JNDI from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling JNDI from JSP" Watch "Calling JNDI from JSP" New topic
Author

Calling JNDI from JSP

Yogesh Deshmukh
Greenhorn

Joined: Dec 26, 2000
Posts: 20
I want to authenticate a user from the login page using a JSP-JNDI-LDAP combination.
i am using the iPlanet Directory server which comes with the iPlanet App server 6.0 Evaluation version.
The JNDI-LDAP code gives me the authentication but hen combined with JSP, I am not able to get any output.
What do I do?
Please help.
Thanks
Yogesh
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Please post the relevant code and a little bit more description of the problem.
Yogesh Deshmukh
Greenhorn

Joined: Dec 26, 2000
Posts: 20
Here the variables strUsername and strPassword are entered on the login html page which are passed to JSP and then to JNDI-LDAP code given below.
I replaced the above two variables with a specific information which I had created in the directory of the Server , the code then works.
import java.util.Hashtable;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.naming.directory.*;
public class JNDISearch
{
// initial context implementation
public static final String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
public static final String MY_HOST = "ldap://wnctg53.yogesh.com:389";
public static String MY_SEARCHBASE = "ou=People,o=yogesh.com";
//specify which attributes we are looking for
public boolean String MY_ATTRS[] = {"uid"};
/*public static void main(String args[])
{
JNDISearch obj = new JNDISearch();
obj.method1();
}*/
public String method1(strUserName, strPassword)
{
boolean flag = true;
String MGR_PW = strPassword;
String MGR_DN = "uid="+strUserName+",ou=People,o=yogesh.com";
String MY_FILTER = "(uid="+strUserName+")";
try
{
//Hashtable for environmental information
Hashtable env = new Hashtable();
//Specify which class to use for our JNDI provider
env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
// Specify host and port to use for directory service
env.put(Context.PROVIDER_URL, MY_HOST);
// Setting the securit level
env.put(Context.SECURITY_AUTHENTICATION, "simple");
// Verifying the username
env.put(Context.SECURITY_PRINCIPAL, MGR_DN);
// Verifying the password
env.put(Context.SECURITY_CREDENTIALS, MGR_PW);
//return flag;
//System.out.println("Hi, you are in method1");
//now step through the search results

//Get a reference to a directory context
DirContext ctx = new InitialDirContext(env);
//specify the scope of the search
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
//perform the actual search
//we give it a searchbase, a filter and constraints
//containing the scope of the search
NamingEnumeration results =
ctx.search(MY_SEARCHBASE, MY_FILTER,constraints);
//now step through the search results
while (results != null && results.hasMore())
{
SearchResult sr = (SearchResult) results.next();
String dn = sr.getName() + "," + MY_SEARCHBASE;
//System.out.println("Distinguished Name is "+dn);
Attributes ar = ctx.getAttributes(dn, MY_ATTRS);
if (ar == null)
{
flag = false;
}
else {
for (int i =0;i<MY_ATTRS.length;i++)
{
Attribute attr = ar.get(MY_ATTRS[i]);
String check = (String)attr.get();
if ( check.equalsIgnoreCase(strUserName))
{
flag = true;
}
else
{
flag = false;
}
}
}
}

}
catch(Exception e)
{
e.printStackTrace();
}
//System.out.println("**** The flag : " + flag);
return flag;
}
}
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Please post the jsp code too.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Yogesh,
Have you created an instance of JNDISearch, in your JSP using the <jsp:usebean> action tag?Keep the scope as application.I think your JSP engine is not able to recognize the instance of JSPSearch.
Let me know the error/exception you are getting, if any.
Regards,


------------------
Sandeep Desai
vgdesai@bom3.vsnl.net.in

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Yogesh Deshmukh
Greenhorn

Joined: Dec 26, 2000
Posts: 20
Hello Bhupinder, Sandeep
Following is the code of the JSP:
Please let me know if there are any errors
Thanks
<%@ page import="java.sql.*" %>
<%@ page session="true" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="shoppingcart.*" %>

<HTML>
<HEAD>
<TITLE>
Validating.............
</TITLE>
</HEAD>
<BODY bgcolor=Thistle >
<%
String strUserName = request.getParameter("username");
String strPassword = request.getParameter("password");
JNDISearch call = new JNDISearch();
boolean bRetVal = call.method1(strUserName,strPassword);
if(bRetVal)
{
String strUser = request.getParameter("username");
out.println("<h2>Hello!!!\" + strUser + \"</h2>");
out.println("<h4> What do u want to do??? </h4>");
out.println("<a href=\"getdata.jsp\"> View already selected items? </a><br><br>");
out.println("Choose new items from the following categories <br>");
out.println("<a href=\"javascript:autobio()\"> Autobiographies </a> <br>");
out.println("<a href=\"javascript:charles()\"> Charles Dickens </a> <br>");
out.println("<a href=\"javascript:bestsellers()\"> Best Sellers </a>");
}
else
{
out.println("<p align=center>");
out.println("<b> Invalid User!!! <br>");
out.println("Check ur UserName and Password </b><br><br>");
out.println("<a href=\"../shoppingcart/login.htm\"> Back </a>");
out.println("<p>");
}
%>

<form name=session1 method=get action="charles.jsp">
<input type=hidden name=char id=char value= <%= request.getParameter("username") %> >
</form>
<form name=session2 method=get action="bestsellers.jsp">
<input type=hidden name=char id=char value= <%= request.getParameter("username") %> >
</form>
<form name=session3 method=get action="autobio.jsp">
<input type=hidden name=char id=char value= <%= request.getParameter("username") %> >
</form>
<script>
function charles()
{
document.session1.submit();
//alert(document.session.char.value);
}
function bestsellers()
{
document.session2.submit();
//alert(document.session.char.value);
}
function autobio()
{
document.session3.submit();
//alert(document.session.char.value);
}
</script>

</BODY>
</html>
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Yogesh,
Please add the following line of code in your JSP and let me know, what happens.

You may then remove the following line from the JSP:

The problem could be what I had mentioned in my earlier post,

Have you created an instance of JNDISearch, in your JSP using the <jsp:usebean> action tag?Keep the scope as application.I think your JSP engine is not able to recognize the instance of JSPSearch.

Hope this helps
Regards,

------------------
Sandeep Desai
vgdesai@bom3.vsnl.net.in

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
Yogesh Deshmukh
Greenhorn

Joined: Dec 26, 2000
Posts: 20
Hello Sandeep,
When I press Submit from the HTML page, I get both the outputs i.e. of the IF block as well as the ELSE block.
Also, the validaiton is not taking place.
I added the line you had posted. But with the same result.
Yogesh
if(bRetVal)
{
String strUser = request.getParameter("username");
...
...
out.println("<a href=\"javascript:bestsellers()\"> Best Sellers </a>");
}
else
{
out.println("<p align=center>");
...
...
out.println("<p>");
}
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Originally posted by Yogesh Deshmukh:

boolean bRetVal = call.method1(strUserName,strPassword);

Yogesh, I am surprised that the javac compiler as well as the jsp compiler is not giving you errors. You have declared that your JNDISearch.method1 returns String but you are actually returning a boolean flag in the method??? What's with that? Did you not get any errors?? And even if it did compile the JNDISearch class, then jsp compiler should have given you error b/c method1 returns a String but you are assigning it to a boolean!
Compiler should have refused to compile the jsp! Try fixing it and let us know what happened.
Yogesh Deshmukh
Greenhorn

Joined: Dec 26, 2000
Posts: 20
Hello Bhupinder,
Thanks for the correction.
Even then, the same error as described in my earlier posting continues, with neither of the compilers giving me any error.
The JNDI-LDAP code still runs independently and gives the requisite output, when used with directory-specific entries(specific to my directory server i.e in place of strUserName, I have an entry, say "YDeshmukh" and in place of strPassword, I have an entry, say, "ydeshmukh" in the DN 'ou=People,o=yogesh.com').
But it doesn't take the inputs supplied by the JSP (i.e. strUserName and strPassword)
On the other hand, JSP gives erroneous output - executing both IF and ELSE blocks.
Yogesh
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hello Yogesh,
This might be one more error in your code :
It should be :

instead of


The return type would be boolean, if you have corrected the code as per what Bhupinder, has suggested.
I am really surprised how you are able to compile this code!!!Tell me which compiler are you using, I am very eager to work on it
Regards,
Sandeep Desai
vgdesai@bom3.vsnl.net.in

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56

  5. [This message has been edited by Desai Sandeep (edited April 26, 2001).]
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Originally posted by Yogesh Deshmukh:
Hello Bhupinder,
Thanks for the correction.
Even then, the same error as described in my earlier posting continues

Yogesh, can you post the generated .java file for the jsp?
 
jQuery in Action, 2nd edition
 
subject: Calling JNDI from JSP