首页 > 解决方案 > 为什么我在 IntelliJ IDEA Ultimate 2020.2 的 JSP&servlet 中发现 404 错误?

问题描述

我正在 IntelliJ IDEA Ultimate 2020.2 中的 jsp 和 servlet 中创建简单的登录 Web 应用程序。

以下是我的 index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Calculator</title>
</head>
<body>
<form method="post" action="/LoginServlet">
    UerName:<input type="text" name="t1"><br>
    Password:<input type="text" name="t2"><br>
    <input type="submit" value="Ok"><br>
</form>
</body>
</html>

这是我的小服务程序。它位于 src->main->java->francis

@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Welcome.....");
    }
}

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">

    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>francis.LoginServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>

</web-app>

但是输出屏幕是...

我的输出屏幕

请帮我。提前致谢

标签: jspservletshttp-status-code-404

解决方案


代替

<form method="post" action="/LoginServlet">

<form method="post" action="LoginServlet">

会自动翻译成the-context-path/LoginServlet. 请注意,您放入action属性的路径是相对于上下文路径的。


推荐阅读