Tomcat 10.0.4 无法加载 servlet(@WebServlet 类)并出现 404 错误

发布于 2025-01-10 21:41:05 字数 2839 浏览 1 评论 0原文

我的第一个 Web 应用程序遇到问题。我使用 IntelliJ 作为 IDE,使用 Tomcat 作为 Web 服务器。 我尝试访问的每个 servlet 都会抛出 404 错误。即使我复制了一些 YouTube 教程,这似乎也很有魅力。

表单中的按钮将我发送到:http://localhost:8080/IUBHQuiz/login

你能告诉我出了什么问题吗?我快要疯了。

login.java

package com.example.IUBHQuiz;

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

@WebServlet("/login")
public class login extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String email = request.getParameter("fmail");
        String pass = request.getParameter("fpw");

        if(email.equals("j") && pass.equals("j"))
        {
            RequestDispatcher rs = request.getRequestDispatcher("/main.jsp");
            rs.forward(request, response);
        }
        else
        {
            out.println("Username or Password incorrect");

            RequestDispatcher rs = request.getRequestDispatcher("/index.jsp");
            rs.include(request, response);
        }

        out.close();
    }

index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>IUBH Quiz</title>
    <link href="./resources/css/style.css" rel="stylesheet">
</head>
<body>
    <div class="main">
        <div class="image-container">
           <img src="./resources/images/logo.png" alt="Logo">
        </div>
        <div class="Login">
            <h1>Willkommen beim IUBH-Quiz!</h1>
            <form action="login" method="post">
                E-Mail:<input type="text" id="fmail" name="fmail"><br><br>
                Passwort: <input type="password" id="fpw" name="fpw"><br><br>
                <input type="submit" value="Log In" class="button">
            </form>
        </div>
        <div class="Links">
            <a href="#">Passwort vergessen</a>
            <a href="#">Registrieren</a>
        </div>
    </div>
</body>
</html>

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">
</web-app>

I'm having a problem with my first Web Application. I use IntelliJ as IDE and Tomcat as Webserver.
Every servlet I've tried to acces, throws an 404 Error. Even if I copy some youtube tutorials, which seems to work like a charm.

The button in the form sends me to: http://localhost:8080/IUBHQuiz/login

Can you tell me whats wrong? I am going nuts.

login.java

package com.example.IUBHQuiz;

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

@WebServlet("/login")
public class login extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String email = request.getParameter("fmail");
        String pass = request.getParameter("fpw");

        if(email.equals("j") && pass.equals("j"))
        {
            RequestDispatcher rs = request.getRequestDispatcher("/main.jsp");
            rs.forward(request, response);
        }
        else
        {
            out.println("Username or Password incorrect");

            RequestDispatcher rs = request.getRequestDispatcher("/index.jsp");
            rs.include(request, response);
        }

        out.close();
    }

index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>IUBH Quiz</title>
    <link href="./resources/css/style.css" rel="stylesheet">
</head>
<body>
    <div class="main">
        <div class="image-container">
           <img src="./resources/images/logo.png" alt="Logo">
        </div>
        <div class="Login">
            <h1>Willkommen beim IUBH-Quiz!</h1>
            <form action="login" method="post">
                E-Mail:<input type="text" id="fmail" name="fmail"><br><br>
                Passwort: <input type="password" id="fpw" name="fpw"><br><br>
                <input type="submit" value="Log In" class="button">
            </form>
        </div>
        <div class="Links">
            <a href="#">Passwort vergessen</a>
            <a href="#">Registrieren</a>
        </div>
    </div>
</body>
</html>

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">
</web-app>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年华零落成诗 2025-01-17 21:41:05

由于版权原因,Servlet 5.0 API(由 Tomcat 10 实现)和 Servlet 4.0 API(由 Tomcat 9 实现)不兼容:API 命名空间从 javax.* 更改为 jakarta.*< /代码>。这可以通过多种方式体现出来:

  1. 为 Servlet 4.0 编写的软件无法针对 Tomcat 10 中的 API jar 进行编译:参见。 Servlet 5.0 JAR 在 javax.servlet.* 上引发编译错误,但 Servlet 4.0 JAR 不会
  2. 使用 < 的 Servlet 4.0 应用程序code>web.xml 描述符抛出大量 ClassNotFoundException 并且无法启动:cf. Tomcat 10.x 在 javax/servlet/ServletRequestListener 上抛出 java.lang.NoClassDefFoundError
  3. 使用 web.xml 描述符的 Servlet 4.0 应用程序会记录“X 不是 jakarta.servlet.Servlet” 错误:参见。 Servlet 类 org.restlet.ext.servlet.ServerServlet 不是 jakarta.servlet.Servlet
  4. 使用注释声明 servlet 的 Servlet 4.0 应用程序将停止工作,正如在您的情况中一样,
  5. 依赖于 ServletContainerInitializer 的 Servlet 4.0 应用程序(例如 Spring 和 Spring Boot 应用程序)不会停止 工作开始: 参见在 Tomcat 10 上部署 Spring MVC 5 …部署问题

最后一个是最难诊断的:没有错误写入日志文件,但应用程序无法运行。此行为背后的原因是 @javax.servlet.WebServlet 注释被忽略:服务器正在扫描 @jakarta.servlet.WebServlet

由于这三个问题都有相同的原因,因此针对上述问题提供的解决方案都有效。在这种特定情况下,我建议使用 Jakarta EE 的 Tomcat 迁移工具

备注Tomcat 下载站点有一个警告,不幸的是很多人没有注意到:

Tomcat 10 及以上版本的用户应该注意,作为 Java EE 向 Eclipse 基金会转移的一部分,从 Java EE 迁移到 Jakarta EE 的结果是,所有已实现 API 的主要包已从 javax. * 飞往雅加达。*。这几乎肯定需要更改代码才能使应用程序从 Tomcat 9 及更早版本迁移到 Tomcat 10 及更高版本。

For copyright reasons the Servlet 5.0 API (implemented by Tomcat 10) and the Servlet 4.0 API (implemented by Tomcat 9) are incompatible: the API namespace changed from javax.* to jakarta.*. This can manifest in many ways:

  1. Software written for Servlet 4.0 does not compile against the API jars from Tomcat 10: cf. Servlet 5.0 JAR throws compile error on javax.servlet.* but Servlet 4.0 JAR does not,
  2. Servlet 4.0 applications which use a web.xml descriptor throw a lot of ClassNotFoundExceptions and don't start: cf. Tomcat 10.x throws java.lang.NoClassDefFoundError on javax/servlet/ServletRequestListener.
  3. Servlet 4.0 applications which use a web.xml descriptor log a "X is not a jakarta.servlet.Servlet" error: cf. Servlet class org.restlet.ext.servlet.ServerServlet is not a jakarta.servlet.Servlet.
  4. Servlet 4.0 applications which use annotations to declare servlets stop working, as in your case,
  5. Servlet 4.0 applications which rely on a ServletContainerInitializer (like Spring and Spring Boot applications) don't start: cf. Deploying Spring MVC 5 on Tomcat 10 … deployment problems

The last one is the hardest to diagnose: no errors are written to the log files, but the application doesn't work. The reason behind this behavior is that @javax.servlet.WebServlet annotations are ignored: the server is scanning for @jakarta.servlet.WebServlet.

Since all three problems have the same cause, the solutions provided to the aforementioned questions all work. In this specific case I would advise to use the Tomcat Migration Tool for Jakarta EE.

Remark: The Tomcat download site features a warning, that unfortunately many people don't notice:

Users of Tomcat 10 onwards should be aware that, as a result of the move from Java EE to Jakarta EE as part of the transfer of Java EE to the Eclipse Foundation, the primary package for all implemented APIs has changed from javax.* to jakarta.*. This will almost certainly require code changes to enable applications to migrate from Tomcat 9 and earlier to Tomcat 10 and later.

不及他 2025-01-17 21:41:05

I had the same issue while reproducing the problem reported at IntelliJ IDEA forums.

It didn't work with Tomcat 10 for the reasons described in the answer by Piotr P. Karwasz, but it works just fine with Tomcat 9.0.44 and earlier versions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文