Tomcat 10.0.4 无法加载 servlet(@WebServlet 类)并出现 404 错误
我的第一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于版权原因,Servlet 5.0 API(由 Tomcat 10 实现)和 Servlet 4.0 API(由 Tomcat 9 实现)不兼容:API 命名空间从
javax.*
更改为jakarta.*< /代码>。这可以通过多种方式体现出来:
ClassNotFoundException
并且无法启动:cf. Tomcat 10.x 在 javax/servlet/ServletRequestListener 上抛出 java.lang.NoClassDefFoundError。web.xml
描述符的 Servlet 4.0 应用程序会记录“X 不是 jakarta.servlet.Servlet” 错误:参见。 Servlet 类 org.restlet.ext.servlet.ServerServlet 不是 jakarta.servlet.Servlet。ServletContainerInitializer
的 Servlet 4.0 应用程序(例如 Spring 和 Spring Boot 应用程序)不会停止 工作开始: 参见在 Tomcat 10 上部署 Spring MVC 5 …部署问题最后一个是最难诊断的:没有错误写入日志文件,但应用程序无法运行。此行为背后的原因是
@javax.servlet.WebServlet
注释被忽略:服务器正在扫描@jakarta.servlet.WebServlet
。由于这三个问题都有相同的原因,因此针对上述问题提供的解决方案都有效。在这种特定情况下,我建议使用 Jakarta EE 的 Tomcat 迁移工具。
备注:Tomcat 下载站点有一个警告,不幸的是很多人没有注意到:
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.*
tojakarta.*
. This can manifest in many ways:web.xml
descriptor throw a lot ofClassNotFoundException
s and don't start: cf. Tomcat 10.x throws java.lang.NoClassDefFoundError on javax/servlet/ServletRequestListener.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.ServletContainerInitializer
(like Spring and Spring Boot applications) don't start: cf. Deploying Spring MVC 5 on Tomcat 10 … deployment problemsThe 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:
我在重现 问题时遇到了同样的问题IntelliJ IDEA 论坛。
由于 Piotr P. Karwasz 的答案中描述的原因,它无法在 Tomcat 10 上运行,但它运行得很好Tomcat 9.0.44 及更早版本。
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.