问题描述
我的第一个 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>
解决方法
出于版权原因,Servlet 5.0 API(由 Tomcat 10 实现)和 Servlet 4.0 API(由 Tomcat 9 实现)不兼容:API 命名空间从 javax.*
更改为 jakarta.*
。这可以通过多种方式表现出来:
- 为 Servlet 4.0 编写的软件不能针对 Tomcat 10 的 API jar 进行编译:参见。 Servlet 5.0 JAR throws compile error on javax.servlet.* but Servlet 4.0 JAR does not,
- 使用
web.xml
描述符的 Servlet 4.0 应用程序会抛出很多ClassNotFoundException
并且无法启动:参见。 Tomcat 10.x throws java.lang.NoClassDefFoundError on javax/servlet/ServletRequestListener。 - 使用
web.xml
描述符的 Servlet 4.0 应用程序记录 “X 不是 jakarta.servlet.Servlet” 错误:参见。 Servlet class org.restlet.ext.servlet.ServerServlet is not a jakarta.servlet.Servlet。 - 使用注释来声明 servlet 停止工作的 Servlet 4.0 应用程序,如在您的情况下。
最后一个是最难诊断的:没有错误写入日志文件,但应用程序无法运行。这种行为背后的原因是 @javax.servlet.WebServlet
注释被忽略:服务器正在扫描 @jakarta.servlet.WebServlet
。
由于所有三个问题的原因相同,因此针对上述问题提供的解决方案均有效。在这种特定情况下,我建议使用 Tomcat Migration Tool for Jakarta EE。
备注:Tomcat download site 有一个警告,可惜很多人没有注意到:
Tomcat 10 以后的用户应该知道,作为 Java EE 向 Eclipse Foundation 转移的一部分,从 Java EE 转移到 Jakarta EE,所有已实现的 API 的主要包已从 javax 更改。 * 到雅加达。*。这几乎肯定需要更改代码才能使应用程序从 Tomcat 9 及更早版本迁移到 Tomcat 10 及更高版本。
另见
,我在复制 the problem reported at IntelliJ IDEA forums 时遇到了同样的问题。
由于 described in the answer by Piotr P. Karwasz 的原因,它不适用于 Tomcat 10,但它适用于 Tomcat 9.0.44 及更早版本。