Issue
When trying to launch the application, error 500 (HTTP Status 500 – Internal Server Error) appears when it comes to accessing the ControllerServlet, the code of which is presented below
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);
}
}
}
Text of error:
Type Exception Report
Message Класс [com.example.ControllerServlet] не является сервлетом
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
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) Root Cause
java.lang.ClassCastException: class com.example.ControllerServlet cannot be cast to class jakarta.servlet.Servlet (com.example.ControllerServlet is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @7abda5a1; jakarta.servlet.Servlet is in unnamed module of loader 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)
Version of Tomcat: 10.0.14
My question is: how to avoid this error and what needs to be fixed?
Solution
The issue is here:
java.lang.ClassCastException: class com.example.ControllerServlet cannot be cast to class jakarta.servlet.Servlet
here:
import javax.servlet.ServletException;
import javax.servlet.http.*;
and here:
Version of Tomcat: 10.0.14
You have a javax
servlet in a servlet container that implements version 5 of the servlet specification which is now in the jakarta
namespace.
You need to extend your servlet from the jakarta
packages, not javax
.
See the following links for more details:
- https://tomcat.apache.org/whichversion.html
- https://en.wikipedia.org/wiki/Jakarta_Servlet
- https://dzone.com/articles/jakarta-ee-without-javax-the-world-wont-end-this-time-either
Answered By - Bogdan
Answer Checked By - Cary Denson (JavaFixing Admin)