How to rectify the following error while using Servlet?
I am learning Servlets concept. Initially, I Tutorial referred to this link and working on the HelloWorld example.
On submission of the JSP form with the name and age I get the following errors. Kindly advise on what has to be done.
The locations of my files placed are as follows,
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\servlete开发者_Python百科xmple\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)
My web.xml file contains as follows,
<?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>
You must put your class files in webapps/servletexmple/WEB-INF/classes
. There you should follow the package structure. I.e. place the file in
webapp/servletexample/WEB-INF/classes/example/HelloServlet
in your web.xml you should use the exact fully qualified name of the servlet. I.e. example.HelloServlet
. For more info about packages in Java see here
Your root cause tells you that example.HelloServlet
isn't found.
That's because, in your web.xml, you're never declared example.HelloServlet
.
Change your current declaration:
<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>
精彩评论