My class is not a servlet error
I have the following servlet code
public void doPost(HttpServletRequest request, HttpServletResponse response){
Backup bup = new Backup();
bup.doBackup();
response.setContentType("text/html");
PrintWriter out;
try {
out = response.getWriter();
out.println("You backed up your data - well hopefully");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And the following stack trace
javax.servlet.ServletException: Class view.BackupServlet is not a Servlet org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454) java.lang.Thread.run(Thread.java:619)
root cause
java.lang.ClassCastException: view.BackupServlet cannot be cast to javax.servlet.Servlet org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454) java.lang.Thread.run(Thread.java:619)
and the web.xml is
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-<br>app_2_4.xsd">
<display-name>Backup</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>BackupServlet</description>
<display-name>BackupServlet</display-name>
<servlet-name>BackupServlet</servlet-name><br>
<servlet-class>view.BackupServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BackupServlet</servlet-name>
<url-pattern>/BackupServlet</url-pattern>
</servlet-mapping>
</web-app>`
You haven't shown your class declaration - my guess is that your class doesn't extend Servlet
or HttpServlet
.
If it does, then check how many different servlet.jar files you have in your deployment - it could be that it's being loaded by two different classloaders.
包含doPost
方法的类必须扩展javax.servlet.Servlet
,但最好使用javax.servlet.HttpServlet
public class BackupServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response) {
.
.
.
}
}
This is most likely you have multiple versions of servlet libraries(.jar files) being loaded my the server. If you are using maven/gradle as build tool make sure you change the scope of the servlet library to scope if it is meant to be provided by the server.
链接地址: http://www.djcxy.com/p/46228.html上一篇: “NoClassDefFoundError:无法初始化类”错误
下一篇: 我的课不是一个servlet错误