有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Tomcat7中的Java 1.5 war产生InvokerServlet异常?

嗨。。我已经部署了我的系统。托马卡特战争。x、 因为我使用了java1。5.当时JAVA_HOME=java5path,CATALINA_HOME=tomcat5。以CentOS为单位的x路径。它工作得很好

现在我的问题是

我在Tomcat7部署同样的战争。带有JAVA_HOME=java1的x。7和CATALINE_HOME=tomcat7。我得到了以下错误

java.lang.ClassNotFoundException: org.apache.catalina.servlets.InvokerServlet
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)...

我还用java1编译了代码。7,并部署在Tomcat7中。。同样的错误。有谁能建议我如何克服这个问题吗

谢谢


共 (3) 个答案

  1. # 2 楼答案

    由于Tomcat7支持新的Servlet标准版本3.0,我们必须注册内部使用的所有Servlet类。这是出于安全目的

    Tomcat6及以下版本,web。xml

    <servlet>
        <servlet-name>invoker</servlet-name>
        <servlet-class>
          org.apache.catalina.servlets.InvokerServlet
        </servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>invoker</servlet-name>
        <url-pattern>/servlet/*</url-pattern>
    </servlet-mapping>
    

    Tomcat 7及以上版本

    <listener>
        <listener-class>
            com.test.MyServletContextListener
        </listener-class>
    </listener>
    <context-param>
        <param-name>com.package</param-name>
        <param-value>
            com.test.servlet,
            com.example.application,
         </param-value>
        <description>servlet implemented packages</description>
    </context-param>
    

    在MyServletContextListener中。java扩展了ServletContextListener,并使用(重写)contextInitialized方法获取所有类和注册,如下面的示例代码段

    for (Class clazz : classes) {
            String mapping = prefix + clazz.getName();
            ServletRegistration sr = sc.addServlet(clazz.getName(), clazz.getName());
            sr.addMapping(mapping);
        }
    
  2. # 3 楼答案

    我在从Tomcat6迁移到Tomcat7时遇到了这个问题。Ricky over-here,改进了一个几乎对我有效的解决方案。我必须改进它才能为我工作。我给他的听众换了一个小赌注,效果很好。我希望它能帮助其他人:

        
        import java.io.File;
        import java.io.IOException;
        import java.net.JarURLConnection;
        import java.net.URL;
        import java.util.Enumeration;
        import java.util.HashSet;
        import java.util.Set;
        import java.util.jar.JarEntry;
        import java.util.jar.JarFile;
        import javax.servlet.ServletContext;
        import javax.servlet.ServletContextEvent;
        import javax.servlet.ServletContextListener;
        import javax.servlet.ServletRegistration;
        import javax.servlet.http.HttpServlet;
        import org.apache.catalina.ContainerServlet;
    
        /**
         *
         * http://snippets.dzone.com/posts/show/4831
         * 
         * @author ricky
         */
        public class InvokerLoadListener implements ServletContextListener {
    
            /**
             * Invoker parameter that defines the packages to search servlets.
             * Comma separated list of packages
             */
            public static final String PACKAGES_PARAMETER = "invoker.packages";
            
            /**
             * Invoker parameter to setup the mapping name. By default is "/servlet/"
             */
            public static final String INVOKER_PREFIX_PARAMETER = "invoker.prefix";
    
            /**
             * Scans all classes accessible from the context class loader which 
             * belong to the given package and subpackages.
             * 
             * @param packageName
             * @return The list of classes found
             */
            private Set getClasses(String packageName) {
                Set classes = new HashSet();
                try {
                    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
                    String path = packageName.replace('.', '/');
                    Enumeration resources = classLoader.getResources(path);
                    while (resources.hasMoreElements()) {
                        URL resource = resources.nextElement();
                        if (resource.getProtocol().equals("jar")) {
                            // inside a jar => read the jar files and check
                            findClassesJar(resource, path, classes);
                        } else if (resource.getProtocol().equals("file")) {
                            // read subdirectories and find
                            findClassesFile(new File(resource.getFile()), packageName, classes);
                        } else {
                            System.err.println("Unknown protocol connection: " + resource);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return classes;
            }
    
            /**
             * Reads a jar file and checks all the classes inside it with the package
             * name specified.
             * 
             * @param resource The resource url
             * @param path
             * @param classes
             * @return 
             */
            private Set findClassesJar(URL resource, String path, Set classes) {
                JarURLConnection jarConn = null;
                JarFile jar = null;
                try {
                    jarConn = (JarURLConnection) resource.openConnection();
                    jar = jarConn.getJarFile();
                    Enumeration e = jar.entries();
                    while (e.hasMoreElements()) {
                        JarEntry entry = e.nextElement();
                        if ((entry.getName().startsWith(path + "/")
                                || entry.getName().startsWith(path + "."))
                                && entry.getName().endsWith(".class")) {
                            String name = entry.getName().replace('/', '.');
                            name = name.substring(0, name.length() - 6);
                            checkClass(name, classes);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        jar.close();
                    } catch (IOException e) {
                    }
                }
                return classes;
            }
    
            /**
             * Recursive method used to find all classes in a given file (file
             * or directory).
             *
             * @param file   The base directory
             * @param packageName The package name for classes found inside the base directory
             * @ classes The list of classes
             * @return The same classes
             * @throws ClassNotFoundException
             */
            private Set findClassesFile(File file, String packageName, Set classes) {
                if (file.isFile() && file.getName().endsWith(".class")) {
                    //classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
                    checkClass(packageName.substring(0, packageName.length() - 6), classes);
                } else {
                    File[] files = file.listFiles();
                    if ( files != null ){
                        for (File f : files) {
                            findClassesFile(f, packageName + "." + f.getName(), classes);
                        }
                    }
                }
                return classes;
            }
    
            private Set checkClass(String name, Set classes) {
                try {
                    Class clazz = Class.forName(name);
                    //Changed the AND to an OR
                    if (HttpServlet.class.isAssignableFrom(clazz)
                            || ContainerServlet.class.isAssignableFrom(clazz)) {
                        classes.add(clazz);
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                return classes;
            }
    
            @Override
            public void contextInitialized(ServletContextEvent sce) {
                System.out.println("contextInitialized(ServletContextEvent e)");
                ServletContext sc = sce.getServletContext();
                String list = sc.getInitParameter(PACKAGES_PARAMETER);
                String prefix = sc.getInitParameter(INVOKER_PREFIX_PARAMETER);
                if (prefix == null) {
                    prefix = "/servlet/";
                }
                if (list != null) {
                    String[] packages = list.split(",");
                    for (int i = 0; i  0) {
                            System.out.println("Checking package: " + packageName);
                            // load classes under servlet.invoker
                            Set classes = getClasses(packageName);
                            System.out.println("size: " + classes.size());
                            for (Class clazz : classes) {
                                String mapping = prefix + clazz.getName();
                                System.out.println("Adding '" + clazz.getName() + "' in mapping '" + mapping + "'");
                                ServletRegistration sr = sc.getServletRegistration(clazz.getName());
                                //Changed this; it seems sometime the servlets are already registered, and needs remapping
                                if ( sr == null )
                                     sr = sc.addServlet(clazz.getName(), clazz.getName());
                                if ( sr != null )
                                   sr.addMapping(mapping);
                            }
                        }
                    }
                }
            }
    
            @Override
            public void contextDestroyed(ServletContextEvent sce) {
                System.out.println("contextDestroyed(ServletContextEvent e)");
            }
        }
    

    干杯