jakarta.servlet.ServletException: Class [com.practice.MyServlet] 不是 Servlet [英] jakarta.servlet.ServletException: Class [com.practice.MyServlet] is not a Servlet

查看:37
本文介绍了jakarta.servlet.ServletException: Class [com.practice.MyServlet] 不是 Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用 servlet 的初学者.
我正在创建一个 servlet,但在运行后出现错误
谁能帮我解决这个问题.

错误

I am beginner to working with servlet.
I am creating a servlet and but after running that i am getting error
Can anyone help me to resolve this.

Error

输入异常报告

消息类 [com.practice.MyServlet] 不是 Servlet

描述服务器遇到意外情况阻止它完成请求.

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

异常

jakarta.servlet.ServletException: Class [com.practice.MyServlet] 是不是 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) 根本原因

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 不能转换为jakarta.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) 注意完整的堆栈跟踪服务器日志中提供了根本原因.

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.

点击此处查看项目目录

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) 或更高版本.这根本不是正确的方法.

You're basically physically including Tomcat 9.x (Servlet 4.0) specific JAR file in WAR and then writing/compiling code against Tomcat 9.x (Servlet 4.0) or older and then then deploying the WAR to Tomcat 10.x (Servlet 5.0) or newer. This is not the correct approach at all.

自 Tomcat 10.x (Servlet 5.0) 起,javax.* 包已重命名为 jakarta.* 包.

Since Tomcat 10.x (Servlet 5.0) the javax.* package has been renamed to jakarta.* package.

换句话说,请确保您不要将不同服务器的 JAR 文件随意放入您的 WAR 中,例如 tomcat-servlet-api-9.0.4.jar.这只会带来麻烦.完全删除它并编辑您的 servlet 类的导入

In other words, please make sure that you don't randomly put JAR files of a different server in your WAR such as tomcat-servlet-api-9.0.4.jar. This will only cause trouble. Remove it altogether and edit the imports of your servlet class from

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

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

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

While at it, please also make sure that the root element of the web.xml is declared conform the Servlet API version of the target server, which is in case of Tomcat 10.x thus Servlet 5.0 (and thus not Servlet 3.1 which basically matches 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>

另见:

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆