Servlet class X is not a javax.servlet.Servlet解决方案

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

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值