使用Servlet时如何纠正以下错误?

时间:2010-09-13 13:59:00

标签: java jsp servlets

我正在学习Servlets的概念。最初,我Tutorial引用此链接并处理HelloWorld示例。

在提交带有名称和年龄的JSP表单时,我收到以下错误。请告知必须做些什么。

我放置文件的位置如下,

C:\ Program Files \ Apache Software Foundation \ Tomcat 5.5 \ webapps \ servletexmple \ hello.jsp

C:\ Program Files \ Apache Software Foundation \ Tomcat5.5 \ webapps \ servletexmple \ example \ HelloServlet.class

C:\ Program Files \ Apache Software Foundation \ Tomcat 5.5 \ webapps \ servletexmple \ WEB-INF \ web.xml

Exception:

javax.servlet.ServletException: Wrapper cannot find servlet class example.HelloServlet or a class it depends on
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
java.lang.Thread.run(Unknown Source)

Root Cause:
java.lang.ClassNotFoundException: example.HelloServlet
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1359)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1205)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
java.lang.Thread.run(Unknown Source)

我的web.xml文件包含如下,

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>example.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/servletexmple</url-pattern>
</servlet-mapping>
</web-app>

2 个答案:

答案 0 :(得分:2)

您必须将您的班级文件放在webapps/servletexmple/WEB-INF/classes中。你应该遵循包结构。即将文件放在

  

web应用程序/ servletexample / WEB-INF /类/实例/ HelloServlet

在您的web.xml中,您应该使用servlet的完全限定名称。即example.HelloServlet。有关Java中包的更多信息,请参阅here

答案 1 :(得分:1)

您的根本原因告诉您找不到example.HelloServlet

那是因为,在你的web.xml中,你永远不会被宣布为example.HelloServlet

更改您当前的声明:

<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>classes.HelloServlet</servlet-class>
</servlet>


to:

<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>example.HelloServlet</servlet-class>
</servlet>