我试图让我的servlet运行,我已经搜索了2天,并尝试了所有可能的解决方案,但没有运气。
servet类位于相应的文件夹中(即在包名称下)。
我还将servlet中需要的jar文件添加到lib文件夹中。
web.xml文件映射URL并定义servlet。
所以我做了文档中的所有内容,wt的人在这里说,仍然出现此错误:

    type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error instantiating servlet class assign1a.RPCServlet
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
    org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:282)
    org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:357)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1687)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:619)
root cause

java.lang.NoClassDefFoundError: assign1a/RPCServlet (wrong name: server/RPCServlet)
    java.lang.ClassLoader.defineClass1(Native Method)
    java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
    java.lang.ClassLoader.defineClass(ClassLoader.java:616)
    java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2820)
    org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1143)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1638)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
    org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:282)
    org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:357)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1687)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:619)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.5 logs.

这也是我的servlet代码:
package assign1a;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lib.jsonrpc.RPCService;

public class RPCServlet extends HttpServlet {

        /**
         *
         */
        private static final long serialVersionUID = -5274024331393844879L;


        private static final Logger log = Logger.getLogger(RPCServlet.class.getName());

        protected RPCService service = new ServiceImpl();

        public void doGet(HttpServletRequest request, HttpServletResponse response)
                        throws IOException, ServletException {
                response.setContentType("text/html");
                response.getWriter().write("rpc service " + service.getServiceName() +  " is running...");
        }

        public void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws IOException, ServletException {

                try {
                        service.dispatch(request, response);
                } catch (Throwable t) {
                        log.log(Level.WARNING, t.getMessage(), t);
                }
        }

}

请帮我 :)
谢谢。

编辑:这是我的web.xml文件的内容
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">


    <servlet>
        <servlet-name>jsonrpc</servlet-name>
        <servlet-class>assign1a.RPCServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>jsonrpc</servlet-name>
        <url-pattern>/rpc</url-pattern>
    </servlet-mapping>

</web-app>

最佳答案

我在错误消息中注意到短语(wrong name: server/RPCServlet)

这通常意味着类文件的包名称与jar/war中的目录结构不同。

也许您最初是将RPCServlet.java放在名为server的目录下,然后又将RPCServlet.java移至assign1a

或原始包装定义为:

package server;

然后您将其更改为
package assign1a;

无论哪种方法,建议您删除所有已编译的内容(类文件,war文件等)。停止tomcat,然后删除<TOMCAT_HOME>/work目录,以确保一切都消失了。

然后,检查以确保绝对确保RPCServlet.java位于名为assign1a的文件夹中,并确保该程序包定义为:
package assign1a;

重新编译/重建 war ,您应该处于更好的状态。也许尝试在Eclipse(或Netbeans)中发动 war ,因为那样一来,它将不允许您编译到与类文件包名称不匹配的文件夹中。

祝你好运。
-戴夫

10-02 04:59