title: ClassNotFoundException thrown in servlet doPost method
body: <p>I'm new in applet/servlet development, and I'm facing a problem that I haven't found a fix for.</p>
  
  <p>Environment is the following:<br>
  OS:  Solaris 5.10<br>
  AS:  iPlanet6 sp8<br>
  JRE: j2re1.4.2_04</p>
  
  <p>jsse.jar is included in servlet classpath.</p>
  
  <p>My servlet must connect to a ldap server via ldaps; my ldap class looks as follows</p>
  
  <pre><code>public class MyLdapClass
  {
      private static final String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
  
      public MyLdapClass(String _strHost, int _iPort, String _strLogin, String _strPassword, String _strBaseDN)
      {
          ...
  
          environ.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
          environ.put(Context.SECURITY_PRINCIPAL, _strLogin);
          environ.put(Context.SECURITY_CREDENTIALS, _strPassword);
  
          String strUrl = "ldap://" + _strHost + ":" + _iPort;
  
          XTrustProvider.install();
          environ.put(Context.SECURITY_PROTOCOL, "ssl");
  
          environ.put(Context.PROVIDER_URL, strUrl);
          environ.put(Context.SECURITY_AUTHENTICATION, "simple");
  
          environ.put("com.sun.jndi.ldap.read.timeout", "5000");
      }
  
      private void connect() throws NamingException
      {
          try
          {
              this.context = new InitialLdapContext(this.environ, null);
          }
          catch (NamingException ne)
          {
              throw ne;
          }
      }
  
      private void _disconnect() throws NamingException
      {
          try
          {
              this.context.close();
          }
          catch (NamingException ne)
          {
              throw ne;
          }
      }
  }
  </code></pre>
  
  <p>My servlet (portion of code that is related to my problem) looks as follows:</p>
  
  <pre><code>public class MyServlet extends HttpServlet
  {
    MyLdapClass ldap;
  
    public void init(ServletConfig paramServletConfig) throws ServletException
    {
      ldap = new MyLdapClass("ipaddress", port, "login", "pwd", "baseDN");
    }
  
    public void doPost(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
      throws ServletException
    {
      try
      {
          ldap.connect();
      }
      catch (NamingException ne)
      {
        throw ne;
      }
  
      ...
    }
  }
  </code></pre>
  
  <p>When I post a request from applet to the servlet, an exception is thrown that looks like the following one:</p>
  
  <pre><code>javax.naming.CommunicationException: _ip:port_ [**Root exception is java.lang.ClassNotFoundException: javax/net/ssl/SSLSocketFactory**]
     at com.sun.jndi.ldap.Connection.&lt;init&gt;(Unknown Source)
     at com.sun.jndi.ldap.LdapClient.&lt;init&gt;(Unknown Source)
     at com.sun.jndi.ldap.LdapClient.getInstance(Unknown Source)
     at com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
     at com.sun.jndi.ldap.LdapCtx.&lt;init&gt;(Unknown Source)
     at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(Unknown Source)
     at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Unknown Source)
     at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(Unknown Source)
     at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(Unknown Source)
     at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
     at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
     at javax.naming.InitialContext.init(Unknown Source)
     at javax.naming.ldap.InitialLdapContext.&lt;init&gt;(Unknown Source)
     at MyLdapClass.connect(MyLdapClass.java:_row number_)
     at MyServlet.doPost(MyServlet.java:_row number_)
     at javax.servlet.http.HttpServlet.doPost(HttpServlet.java:_row number_)
     at com.iplanet.server.http.servlet.NSServletRunner.invokeServletService(NSServletRunner.java:937)
     at com.iplanet.server.http.servlet.NSServletRunner.Service(NSServletRunner.java:494)
  Caused by: java.lang.ClassNotFoundException: javax/net/ssl/SSLSocketFactory
     at java.lang.Class.forName0(Native Method)
     at java.lang.Class.forName(Unknown Source)
     at com.sun.jndi.ldap.VersionHelper12.loadClass(Unknown Source)
     at com.sun.jndi.ldap.Connection.createSocket(Unknown Source)
     ... 20 more
  Internal error: servlet service function had thrown ServletException (uri=/servlet/MyServlet): javax.servlet.ServletException: NamingException:__ip:port__, stack: javax.servlet.ServletException: NamingException:_ip:port_
  </code></pre>
  
  <p>The same occurs if I call ldap.connect() from Myservlet <em>service</em> method; instead, if I call ldap.connect() from <strong>init</strong> method of MyServlet class, everything works fine, but I have the requirement that connection must be opened on demand, and closed when request has been processed.</p>
  
  <p>Am I missing something? How can I fix it? Thank you all in advance</p>
  
  <p>Just to be complete, XTrustProvider code is the following:</p>
  
  <pre><code>package jas.frontend;
  
  import java.security.AccessController;
  import java.security.InvalidAlgorithmParameterException;
  import java.security.KeyStore;
  import java.security.KeyStoreException;
  import java.security.PrivilegedAction;
  import java.security.Provider;
  import java.security.Security;
  import java.security.cert.X509Certificate;
  
  import javax.net.ssl.ManagerFactoryParameters;
  import javax.net.ssl.TrustManager;
  import javax.net.ssl.TrustManagerFactorySpi;
  import javax.net.ssl.X509TrustManager;
  
  public final class XTrustProvider extends Provider {
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      private final static String NAME = "XTrustJSSE";
      private final static String INFO = "XTrust JSSE Provider (implements trust factory with truststore validation disabled)";
      private final static double VERSION = 1.0D;
  
      public XTrustProvider() {
          super(NAME, VERSION, INFO);
  
          AccessController.doPrivileged(new PrivilegedAction() {
  
              public Object run() {
                  put("TrustManagerFactory." + TrustManagerFactoryImpl.getAlgorithm(),
                          TrustManagerFactoryImpl.class.getName());
                  return null;
              }
          });
      }
  
      public static void install() {
          if (Security.getProvider(NAME) == null) {
              Security.insertProviderAt(new XTrustProvider(), 2);
              Security.setProperty("ssl.TrustManagerFactory.algorithm", TrustManagerFactoryImpl.getAlgorithm());
          }
      }
  
      public final static class TrustManagerFactoryImpl extends TrustManagerFactorySpi {
  
          public TrustManagerFactoryImpl() {}
  
          public static String getAlgorithm() {
              return "XTrust509";
          }
  
          protected void engineInit(KeyStore keystore) throws KeyStoreException {}
  
          protected void engineInit(ManagerFactoryParameters mgrparams) throws InvalidAlgorithmParameterException {
              throw new InvalidAlgorithmParameterException(XTrustProvider.NAME + " does not use ManagerFactoryParameters");
          }
  
          protected TrustManager[] engineGetTrustManagers() {
              return new TrustManager[] { new X509TrustManager() {
  
                  public X509Certificate[] getAcceptedIssuers() {
                      return null;
                  }
  
                  public void checkClientTrusted(X509Certificate[] certs, String authType) {}
  
                  public void checkServerTrusted(X509Certificate[] certs, String authType) {}
              } };
          }
      }
  }
  </code></pre>