简体   繁体   English

servlet doPost方法中引发的ClassNotFoundException

[英]ClassNotFoundException thrown in servlet doPost method

I'm new in applet/servlet development, and I'm facing a problem that I haven't found a fix for. 我是applet / servlet开发的新手,但遇到的问题是尚未找到解决方案。

Environment is the following: 环境如下:
OS: Solaris 5.10 操作系统:Solaris 5.10
AS: iPlanet6 sp8 AS:iPlanet6 sp8
JRE: j2re1.4.2_04 JRE:j2re1.4.2_04

jsse.jar is included in servlet classpath. jsse.jar包含在servlet类路径中。

My servlet must connect to a ldap server via ldaps; 我的servlet必须通过ldaps连接到ldap服务器。 my ldap class looks as follows 我的ldap类如下

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;
        }
    }
}

My servlet (portion of code that is related to my problem) looks as follows: 我的servlet(与我的问题相关的部分代码)如下所示:

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;
    }

    ...
  }
}

When I post a request from applet to the servlet, an exception is thrown that looks like the following one: 当我将小程序的请求发布到servlet时,抛出了一个异常,看起来像下面的异常:

javax.naming.CommunicationException: _ip:port_ [**Root exception is java.lang.ClassNotFoundException: javax/net/ssl/SSLSocketFactory**]
   at com.sun.jndi.ldap.Connection.<init>(Unknown Source)
   at com.sun.jndi.ldap.LdapClient.<init>(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.<init>(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.<init>(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_

The same occurs if I call ldap.connect() from Myservlet service method; 如果我从Myservlet 服务方法调用ldap.connect(),也会发生同样的情况; instead, if I call ldap.connect() from init 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. 相反,如果我从MyServlet类的init方法调用ldap.connect(),则一切正常,但是我要求必须按需打开连接,并在处理请求后关闭连接。

Am I missing something? 我想念什么吗? How can I fix it? 我该如何解决? Thank you all in advance 谢谢大家

Just to be complete, XTrustProvider code is the following: 为了完整起见,XTrustProvider代码如下:

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) {}
            } };
        }
    }
}

Finally I fixed it (thanks to Emanuele Gallo, not signed on SO, but a great guy anyway)!!! 最终,我修复了它(感谢Emanuele Gallo,虽然未签约,但还是个好人)!!!

Problem was that I added 问题是我添加了

/opt/wbp/j2re1.4.2_04/lib/rt.jar:/opt/wbp/j2re1.4.2_04/lib/jce.jar:/opt/wbp/j2re1.4.2_04/lib/jsse.jar /opt/wbp/j2re1.4.2_04/lib/rt.jar:/opt/wbp/j2re1.4.2_04/lib/jce.jar:/opt/wbp/j2re1.4.2_04/lib/jsse.jar

in Classpath field (see picture below) 类路径字段中(请参见下图)

在此处输入图片说明

In order to work, instead, they must be added in Option field with -X option using bootclasspath property. 为了正常工作,必须使用bootclasspath属性将它们与-X选项一起添加到Option字段中。

In othe rwords, in <iPlanet_install_path>/servers/https-<virtual_server_name>/config/jvm12.conf this was the wrong configuration 换句话说,在<iPlanet_install_path> / servers / https- <virtual_server_name> /config/jvm12.conf中,这是错误的配置

#jvm.option=-Xbootclasspath:/lib/tools.jar:/jre/lib/rt.jar #jvm.option = -Xbootclasspath:/lib/tools.jar:/jre/lib/rt.jar
jvm.option=-Xrs jvm.option = -Xrs
jvm.option=-Xnoagent jvm.option = -Xnoagent
jvm.classpath=/opt/iplanet6/servers/plugins/servlets/examples/legacy/beans.10/SDKBeans10.jar:/opt/wbp/j2re1.4.2_04/lib/rt.jar:/opt/wbp/j2re1.4.2_04/lib/jce.jar:/opt/wbp/j2re1.4.2_04/lib/jsse.jar jvm.classpath = / opt / iplanet6 / servers / plugins / servlets / examples / legacy / beans.10 / SDKBeans10.jar:/opt/wbp/j2re1.4.2_04/lib/rt.jar:/ opt / wbp / j2re1。 4.2_04 / lib / jce.jar:/opt/wbp/j2re1.4.2_04/lib/jsse.jar

that worked absolutely fine when replaced with the following one 当替换为下面的一个时,绝对可以正常工作

jvm.option=-Xrs jvm.option = -Xrs
jvm.option=-Xnoagent jvm.option = -Xnoagent
jvm.classpath=/opt/iplanet6/servers/plugins/servlets/examples/legacy/beans.10/SDKBeans10.jar jvm.classpath = / opt / iplanet6 / servers / plugins / servlets / examples / legacy / beans.10 / SDKBeans10.jar
jvm.option=-Xbootclasspath:/opt/wbp/j2re1.4.2_04/lib/rt.jar:/opt/wbp/j2re1.4.2_04/lib/jce.jar:/opt/wbp/j2re1.4.2_04/lib/jsse.jar jvm.option = -Xbootclasspath:/opt/wbp/j2re1.4.2_04/lib/rt.jar:/opt/wbp/j2re1.4.2_04/lib/jce.jar:/opt/wbp/j2re1.4.2_04/lib /jsse.jar

NOTE 1 : each key=value pair must be wtitten on a single line; 注1 :每个键=值对必须在同一行上加权; here newline between '-' and Xbootclasspath.... depends on page formatting 这里的'-'和Xbootclasspath之间的换行符。...取决于页面格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM