我的课不是一个servlet错误

我有以下的servlet代码

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();
    }

}

和下面的堆栈跟踪

javax.servlet.ServletException:类view.BackupServlet不是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)

根本原因

java.lang.ClassCastException:view.BackupServlet不能转换为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)

和web.xml是

<?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>`

你没有显示你的类声明 - 我的猜测是你的类不扩展ServletHttpServlet

如果有,请检查您的部署中有多少个不同的servlet.jar文件 - 可能是它由两个不同的类加载器加载。


包含doPost方法的类必须扩展javax.servlet.Servlet ,但最好使用javax.servlet.HttpServlet

public class BackupServlet extends HttpServlet {
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response) {
     .
     .
     .
    }
}

这很可能是你有多个版本的servlet库(.jar文件)被加载到服务器上。 如果您使用maven / gradle作为构建工具,请确保您将servlet库的范围更改为范围(如果它旨在由服务器提供)。

链接地址: http://www.djcxy.com/p/46227.html

上一篇: My class is not a servlet error

下一篇: Retrieve uploaded relativePath file on server side