0

我试图在 Servlet 中创建一个简单的我的第一个应用程序。我将我的 '/hello' url 映射到 HelloServlet。当我尝试访问 /hello 时,我得到了这个。

    HTTP Status 500 – Internal Server Error
    Type Exception Report

    Message Class [com.example.Servlet3.HelloServlet] is not a Servlet

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

    Exception

    jakarta.servlet.ServletException: Class [com.example.Servlet3.HelloServlet] 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:690)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
        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:870)
        org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1696)
        org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.base/java.lang.Thread.run(Thread.java:834)
    Root Cause

    java.lang.ClassCastException: class com.example.Servlet3.HelloServlet cannot be cast to class jakarta.servlet.Servlet (com.example.Servlet3.HelloServlet is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @6b4c53ee; jakarta.servlet.Servlet is in unnamed module of loader java.net.URLClassLoader @5c5eefef)
        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:690)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
        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:870)
        org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1696)
        org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.base/java.lang.Thread.run(Thread.java:834)
    Note The full stack trace of the root cause is available in the server logs.

    Apache Tomcat/10.0.5

乳清我再次重新加载页面第二次。现在这一次它表明了这一点。

    HTTP Status 404 – Not Found
    Type Status Report

    Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

    Apache Tomcat/10.0.5

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_4_0.xsd"
             version="4.0">
        <display-name>My Application</display-name>
        <servlet>
            <servlet-name>FirstServlet</servlet-name>
            <servlet-class>com.example.Servlet3.HelloServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>FirstServlet</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
    </web-app>

HelloServlet.java 代码。

    package com.example.Servlet3;

    import java.io.*;
    import javax.servlet.http.*;
    import javax.servlet.annotation.*;

    //@WebServlet(name = "helloServlet", value = "/hello-servlet")
    public class HelloServlet extends HttpServlet {
        private String message;

        public void init() {
            message = "Hello World!";
        }

        public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
            response.setContentType("text/html");

            // Hello
            PrintWriter out = response.getWriter();
            out.println("<html><body>");
            out.println("<h1>" + message + "</h1>");
            out.println("</body></html>");
        }

        public void destroy() {
        }
    }

在此处输入图像描述

项目结构 pom.xml 文件。

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>com.example</groupId>
        <artifactId>Servlet3</artifactId>
        <version>1.0-SNAPSHOT</version>
        <name>Servlet3</name>
        <packaging>war</packaging>

        <properties>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.source>1.8</maven.compiler.source>
            <junit.version>5.7.0</junit.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.1</version>

            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>

        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.3.0</version>
                </plugin>
            </plugins>
        </build>
    </project>

我在互联网上搜索了很多,但都是徒劳的。我很沮丧,最后在这里希望得到解决方案。请帮忙。此项目在 IntelliJ New->Project->JavaEnterprise WebApplication(Servlet,web.xml) 中创建,Tomcat 服务器启动无误。它可以提供静态网页,如上图中的“form.html”文件(项目结构),但不能提供任何 servlet。

4

0 回答 0