jakarta.servlet.servletexception:class[com.practice.myservlet]不是servlet

wnrlj8wa  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(450)

我是使用servlet的初学者。
我正在创建一个servlet,但在运行之后,我得到了一个错误
谁能帮我解决这个问题。
错误
类型异常报告
消息类[com.practice.myservlet]不是servlet
说明服务器遇到意外情况,无法满足请求。
例外
jakarta.servlet.servletexception:class[com.practice.myservlet]不是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.niodendpoint$socketprocessor.dorun(niodendpoint.java:1568)org.apache.tomcat.util.net.socketprocessorbase.run(socketprocessorbase.java:49)java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1149)java.util.concurrent.thpoolexecutor$worker.run(threadpoolexecutor.java:624)org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61)java.lang.thread.run(thread.java:748)根本原因
java.lang.classcastexception:com.practice.myservlet不能强制转换为jakarta.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.niodendpoint$socketprocessor.dorun(niodendpoint.java:1568)org.apache.tomcat.util.net.socketprocessorbase.run(socketprocessorbase.java:49)java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1149)java.util.concurrent.thpoolexecutor$worker.run(threadpoolexecutor.java:624)org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61)java.lang.thread.run(thread.java:748)注意根本原因的完整堆栈跟踪在服务器日志中可用。
单击此处查看项目目录
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>
mrzz3bfm

mrzz3bfm1#

您基本上是在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.* 包裹
换句话说,请确保不要在war中随机放置不同服务器的jar文件,例如 tomcat-servlet-api-9.0.4.jar . 这只会带来麻烦。将其全部删除,并从中编辑servlet类的导入

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

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

同时,请确保 web.xml 声明符合目标服务器的ServletAPI版本,在Tomcat10.x中是Servlet5.0(因此不是Servlet3.1,它基本上与Tomcat8.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>

另见:

tomcat版本
tomcat 9将servlet转换为javax.servlet.servlet而不是jakarta.servlet.http.httpservlet

相关问题