gpt4 book ai didi

tomcat - jakarta.servlet.ServletException : Class [com. practice.MyServlet] 不是 Servlet

转载 作者:行者123 更新时间:2023-12-01 23:33:58 25 4
gpt4 key购买 nike

我是使用 servlet 的新手。
我正在创建一个 servlet,但在运行之后出现错误
谁能帮我解决这个问题。

错误

Type Exception Report

Message Class [com.practice.MyServlet] is not a Servlet

Description The server encountered an unexpected condition thatprevented it from fulfilling the request.

Exception

jakarta.servlet.ServletException: Class [com.practice.MyServlet] isnot a Servletorg.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 tojakarta.servlet.Servletorg.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 ofthe 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 文件,然后针对 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 类的导入

import javax.servlet.*;
import javax.servlet.http.*;

import jakarta.servlet.*;
import jakarta.servlet.http.*;

同时,请确保声明的 web.xml 的根元素符合目标服务器的 Servlet API 版本,这是在 Tomcat 10.x 的情况下,因此 Servlet 5.0(因此不是基本上与 Tomcat 8.0 匹配的 Servlet 3.1)。

<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>

另见:

关于tomcat - jakarta.servlet.ServletException : Class [com. practice.MyServlet] 不是 Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65872072/

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