jakarta.servlet.ServletException: Class [com.practice.MyServlet] 不是 Servlet
jakarta.servlet.ServletException: Class [com.practice.MyServlet] is not a Servlet
我刚开始使用 servlet。
我正在创建一个 servlet,但在 运行 之后我遇到了错误
谁能帮我解决这个问题。
错误
Type Exception Report
Message Class [com.practice.MyServlet] is not a Servlet
Description The server encountered an unexpected condition that
prevented it from fulfilling the request.
Exception
jakarta.servlet.ServletException: Class [com.practice.MyServlet] is
not a Servlet
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:682)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:332)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:859)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1568)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:748) Root Cause
java.lang.ClassCastException: com.practice.MyServlet cannot be cast to
jakarta.servlet.Servlet
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:682)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:332)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:859)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1568)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:748) Note The full stack trace of
the root cause is available in the server logs.
Click here to see the Project directory
MyServlet.java
package com.practice;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author admin
*/
public class MyServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{
PrintWriter out=response.getWriter();
response.setContentType("test/html");
out.print("<h1>This is get method of my servlet</h1>");
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>com.practice.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
你基本上在 WAR 中包含 Tomcat 9.x (Servlet 4.0) 特定的 JAR 文件,然后 writing/compiling 针对 Tomcat 9.x (Servlet 4.0) 或更早版本,然后将 WAR 部署到 Tomcat 10.x (Servlet 5.0) 或更新版本。这根本不是正确的做法。
自 Tomcat 10.x (Servlet 5.0) 以来,javax.*
包已重命名为 jakarta.*
包。
换句话说,请确保您不会随意将不同服务器的 JAR 文件放入您的 WAR,例如 tomcat-servlet-api-9.0.4.jar
。这只会带来麻烦。完全删除它并编辑您的 servlet class from
的导入
import javax.servlet.*;
import javax.servlet.http.*;
到
import jakarta.servlet.*;
import jakarta.servlet.http.*;
同时,请确保 web.xml
的根元素声明符合目标服务器的 Servlet API 版本,以防 Tomcat 10.x 因此是 Servlet 5.0(因此 不是 Servlet 3.1,它基本上匹配 Tomcat 8.0)。
<web-app
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0"
>
<!-- Config here. -->
</web-app>
另请参阅:
我刚开始使用 servlet。
我正在创建一个 servlet,但在 运行 之后我遇到了错误
谁能帮我解决这个问题。
错误
Type Exception Report
Message Class [com.practice.MyServlet] is not a Servlet
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
jakarta.servlet.ServletException: Class [com.practice.MyServlet] is not a Servlet org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:682) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:332) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:859) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1568) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:748) Root Cause
java.lang.ClassCastException: com.practice.MyServlet cannot be cast to jakarta.servlet.Servlet org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:682) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:332) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:859) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1568) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:748) Note The full stack trace of the root cause is available in the server logs.
Click here to see the Project directory
MyServlet.java
package com.practice;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author admin
*/
public class MyServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{
PrintWriter out=response.getWriter();
response.setContentType("test/html");
out.print("<h1>This is get method of my servlet</h1>");
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>com.practice.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
你基本上在 WAR 中包含 Tomcat 9.x (Servlet 4.0) 特定的 JAR 文件,然后 writing/compiling 针对 Tomcat 9.x (Servlet 4.0) 或更早版本,然后将 WAR 部署到 Tomcat 10.x (Servlet 5.0) 或更新版本。这根本不是正确的做法。
自 Tomcat 10.x (Servlet 5.0) 以来,javax.*
包已重命名为 jakarta.*
包。
换句话说,请确保您不会随意将不同服务器的 JAR 文件放入您的 WAR,例如 tomcat-servlet-api-9.0.4.jar
。这只会带来麻烦。完全删除它并编辑您的 servlet class from
import javax.servlet.*;
import javax.servlet.http.*;
到
import jakarta.servlet.*;
import jakarta.servlet.http.*;
同时,请确保 web.xml
的根元素声明符合目标服务器的 Servlet API 版本,以防 Tomcat 10.x 因此是 Servlet 5.0(因此 不是 Servlet 3.1,它基本上匹配 Tomcat 8.0)。
<web-app
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0"
>
<!-- Config here. -->
</web-app>