Tomcat 10.0.4 不会加载带有 404 错误的 servlet(@WebServlet 类) [英] Tomcat 10.0.4 doesn't load servlets (@WebServlet classes) with 404 error

查看:53
本文介绍了Tomcat 10.0.4 不会加载带有 404 错误的 servlet(@WebServlet 类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

你能告诉我有什么问题吗?我要疯了.

login.java

package com.example.IUBHQuiz;导入 java.io.*;导入 javax.servlet.*;导入 javax.servlet.annotation.WebServlet;导入 javax.servlet.http.*;导入 java.sql.*;@WebServlet("/登录")公共类登录扩展 HttpServlet {@覆盖protected void doPost(HttpServletRequest 请求,HttpServletResponse 响应)抛出 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(请求,响应);}别的{out.println("用户名或密码不正确");RequestDispatcher rs = request.getRequestDispatcher("/index.jsp");rs.include(请求,响应);}关闭();}

index.jsp

<%@ page contentType="text/html;字符集=UTF-8"pageEncoding="UTF-8";%><!DOCTYPE html><头><title>IUBH测验</title><link href="./resources/css/style.css";rel=样式表"><身体><div class="main"><div class="image-container"><img src="./resources/images/logo.png";alt=徽标">

<div class="登录"><h1>Willkommen beim IUBH-测验!</h1><表单操作=登录"方法=发布">电子邮件:<输入类型=文本"id=邮件"name="fmail"><br><br>密码:<输入类型=密码"id=fpw"名称=fpw"><br><br><输入类型=提交"值=登录"类=按钮"></表单>

<div class="Links"><a href="#">Passwort vergessen</a><a href="#">Registrieren</a>

web.xml

<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"版本=4.0"></web-app>

解决方案

我在复制 IntelliJ IDEA 论坛上报告的问题.

由于在 Piotr P. Karwasz 的回答中描述的原因,它不适用于 Tomcat 10,但它适用于 Tomcat 9.0.44 及更早版本.

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>

解决方案

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.

这篇关于Tomcat 10.0.4 不会加载带有 404 错误的 servlet(@WebServlet 类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆