gpt4 book ai didi

javax.servlet.ServletException : Class com. mq.sample.Hello 不是 Servlet

转载 作者:行者123 更新时间:2023-12-01 09:16:57 24 4
gpt4 key购买 nike

我正在尝试创建简单的登录应用程序。我已经创建了一个登录页面和一个 servlet,但它给出了 ServletException

这是我的示例代码。

public class Hello extends HttpServlet{

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
try{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String username=request.getParameter("username");
String password=request.getParameter("password");
out.println("hello");

if(username.equals("xyz")&&password.equals("password"))
{
HttpSession session=request.getSession();
session.setAttribute("uname",username);
RequestDispatcher rd=request.getRequestDispatcher("/Home.jsp");
rd.forward(request, response);
}
else
{
RequestDispatcher rd=request.getRequestDispatcher("/login.html");
out.println("<h4>Plz provide correct Username or password</h4>");
rd.include(request,response);
}


out.close();

}catch(Exception e){System.out.println(e);}
}

此代码给出以下异常:我不明白为什么会这样。

javax.servlet.ServletException: Class com.mq.sample.Hello 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:857)
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(Unknown Source)
root cause

java.lang.ClassCastException: com.mq.sample.Hello 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:857)
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(Unknown Source)

提前致谢

最佳答案

您从 Tomcat 中收到错误,请参阅 this link 。所以问题是你的类路径中有 servlet jar 冲突。从类路径中删除 javax.servlet-api.jar 并执行此操作。 enter image description here

关于javax.servlet.ServletException : Class com. mq.sample.Hello 不是 Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40480219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com