写servlet的时候,报500错:Servlet class X is not a javax.servlet.Servlet
详细报错:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Class com.servlet.LoginServlet is not a Servlet org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:619)
root cause
java.lang.ClassCastException: com.servlet.LoginServlet cannot be cast to javax.servlet.Servlet org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:619)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.26 logs.
原始代码:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String username=request.getParameter("username");
String password=request.getParameter("password");
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
out.println("<html><body>");
/*if(username.equals("admin")&&password.equals("admin")){
out.println("login successfully!"+username);
}else{
out.println("sorry!Your username or password is uncorrect");
}*/
out.println("haha");
out.println("</body></html>");
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request,response);
}
}
解决方案说明:
写一个servlet时,类需要承继HttpServlet 。
例如:
public class
MyHttpServletRequest extends HttpServlet {
public void doGet(HttpServletRequestrequest, HttpServletResponse response) throws
}
public void doPost(HttpServletRequestrequest, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
}