title: javax.naming.InvalidNameException: [LDAP: error code 34 - invalid DN]
body: <p>I'm a college student. Now, i'm doing a project that must use LDAP connection to authenticate the username and password of the user in log in process. So, my website is develop by use JSP. I try to solve the error of code but i can't. Did I makes some mistake on it?</p>
  
  <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;%@ page import="java.util.*" %&gt;
  &lt;%@ page import="javax.naming.*" %&gt;
  &lt;%@ page import="java.util.regex.*" %&gt;
  &lt;%@ page import="javax.naming.directory.*" %&gt;
  &lt;%@ page import="java.util.Hashtable.*" %&gt;
  &lt;%@ page import="javax.naming.ldap.*" %&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;title&gt;JSP Page&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;%
              String username = request.getParameter("email");
  String password = request.getParameter("password");
              Hashtable&lt;String, String&gt; env = new Hashtable&lt;String, String&gt;();
  env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  env.put(Context.PROVIDER_URL, "ldap://ldap-pj.sit.kmutt.ac.th");
  env.put(Context.SECURITY_AUTHENTICATION, "simple");
  env.put(Context.SECURITY_PRINCIPAL, username);
  env.put(Context.SECURITY_CREDENTIALS, password);
  
  try {
              //Connect with ldap
              new InitialLdapContext(env, null);  
  
              //Connection succeeded
              System.out.println("Connection succeeded!");
          } catch (AuthenticationException e) {
  
              //Connection failed
              System.out.println("Connection failed!");
              e.printStackTrace();
          }  
  %&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and I got this error msg from running the code.</p>
  
  <blockquote>
    <p>HTTP Status 500 -</p>
    
    <p>type Exception report</p>
    
    <p>message</p>
    
    <p>description The server encountered an internal error () that prevented
    it from fulfilling this request.</p>
    
    <p>exception</p>
    
    <p>org.apache.jasper.JasperException: An exception occurred processing
    JSP page /ldap_checking.jsp at line 33</p>
    
    <p>30:  31: try { 32:             //Connect with ldap 33:             new
    InitialLdapContext(env, null);   34:  35:             //Connection
    succeeded 36:             System.out.println("Connection succeeded!");</p>
    
    <p>Stacktrace:
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722) root
    cause</p>
    
    <p>javax.servlet.ServletException: javax.naming.InvalidNameException:
    [LDAP: error code 34 - invalid DN]
      org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
      org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
      org.apache.jsp.ldap_005fchecking_jsp._jspService(ldap_005fchecking_jsp.java:212)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722) root
    cause</p>
    
    <p>javax.naming.InvalidNameException: [LDAP: error code 34 - invalid DN]
      com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:3028)
      com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2835)
      com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2749)
      com.sun.jndi.ldap.LdapCtx.(LdapCtx.java:316)
      com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:193)
      com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:211)
      com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:154)
      com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:84)
      javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
      javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
      javax.naming.InitialContext.init(InitialContext.java:242)
      javax.naming.ldap.InitialLdapContext.(InitialLdapContext.java:153)
      org.apache.jsp.ldap_005fchecking_jsp._jspService(ldap_005fchecking_jsp.java:97)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722) note The
    full stack trace of the root cause is available in the Apache
    Tomcat/7.0.27 logs.</p>
    
    <p>Apache Tomcat/7.0.27</p>
  </blockquote>