访问servlet时出现500错误class
Error 500 occurs when accessing the servlet class
尝试启动应用程序时,在访问 ControllerServlet 时出现错误 500(HTTP 状态 500 – 内部服务器错误),其代码如下所示
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
public class ControllerServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getParameter("x") == null || req.getParameter("y") == null || req.getParameter("r") == null || req.getParameter("key") == null) {
req.getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);
}
else{
getServletContext().getNamedDispatcher("AreaChecker").forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getParameter("key").equals("update")) {
getServletContext().getNamedDispatcher("AreaChecker").forward(req, resp);
}
else{
req.getServletContext().getNamedDispatcher("Controller").forward(req, resp);
}
}
}
错误文本:
类型异常报告
消息 Класс [com.example.ControllerServlet] не является сервлетом
说明服务器遇到意外情况,无法完成请求。
异常
jakarta.servlet.ServletException: Класс [com.example.ControllerServlet] не является сервлетом
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:872)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1705)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:829)
根本原因
java.lang.ClassCastException: class com.example.ControllerServlet 无法转换为 class jakarta.servlet.Servlet (com.example.ControllerServlet 在加载程序 [=76 的未命名模块中=] @7abda5a1; jakarta.servlet.Servlet 在加载程序的未命名模块中 java.net.URLClassLoader @37afeb11)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:872)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1705)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:829)
Tomcat 版本:10.0.14
我的问题是:如何避免这个错误以及需要修复什么?
问题在这里:
java.lang.ClassCastException: class com.example.ControllerServlet cannot be cast to class jakarta.servlet.Servlet
此处:
import javax.servlet.ServletException;
import javax.servlet.http.*;
这里:
Version of Tomcat: 10.0.14
您在 servlet 容器中有一个 javax
servlet,它实现了现在位于 jakarta
命名空间中的 servlet 规范版本 5。
您需要从 jakarta
包中扩展您的 servlet,而不是 javax
。
有关详细信息,请参阅以下链接:
尝试启动应用程序时,在访问 ControllerServlet 时出现错误 500(HTTP 状态 500 – 内部服务器错误),其代码如下所示
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
public class ControllerServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getParameter("x") == null || req.getParameter("y") == null || req.getParameter("r") == null || req.getParameter("key") == null) {
req.getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);
}
else{
getServletContext().getNamedDispatcher("AreaChecker").forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getParameter("key").equals("update")) {
getServletContext().getNamedDispatcher("AreaChecker").forward(req, resp);
}
else{
req.getServletContext().getNamedDispatcher("Controller").forward(req, resp);
}
}
}
错误文本:
类型异常报告
消息 Класс [com.example.ControllerServlet] не является сервлетом
说明服务器遇到意外情况,无法完成请求。
异常
jakarta.servlet.ServletException: Класс [com.example.ControllerServlet] не является сервлетом org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:872) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1705) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.base/java.lang.Thread.run(Thread.java:829) 根本原因
java.lang.ClassCastException: class com.example.ControllerServlet 无法转换为 class jakarta.servlet.Servlet (com.example.ControllerServlet 在加载程序 [=76 的未命名模块中=] @7abda5a1; jakarta.servlet.Servlet 在加载程序的未命名模块中 java.net.URLClassLoader @37afeb11) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:872) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1705) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.base/java.lang.Thread.run(Thread.java:829)
Tomcat 版本:10.0.14
我的问题是:如何避免这个错误以及需要修复什么?
问题在这里:
java.lang.ClassCastException: class com.example.ControllerServlet cannot be cast to class jakarta.servlet.Servlet
此处:
import javax.servlet.ServletException;
import javax.servlet.http.*;
这里:
Version of Tomcat: 10.0.14
您在 servlet 容器中有一个 javax
servlet,它实现了现在位于 jakarta
命名空间中的 servlet 规范版本 5。
您需要从 jakarta
包中扩展您的 servlet,而不是 javax
。
有关详细信息,请参阅以下链接: