You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击JSP提交按钮时doPost方法未触发,Tomcat报404错误

Hey there, let's work through this 404 error step by step—since you're using web.xml instead of annotations, the issue almost always ties back to mapping mismatches or deployment misconfigurations. Here's what to check:

1. Double-Check Servlet Mapping in web.xml

This is the most common culprit. Make sure your <servlet> and <servlet-mapping> blocks are correctly linked, with no typos or case mismatches.

A correct setup should look like this:

<servlet>
    <servlet-name>LoginServlet</servlet-name>
    <!-- Use the FULL package path + class name here -->
    <servlet-class>com.yourpackage.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <!-- This must EXACTLY match your form's action (case-sensitive!) -->
    <url-pattern>/Login</url-pattern>
</servlet-mapping>

Tomcat treats URLs as case-sensitive—if your form uses action="Login" but your mapping uses /login (lowercase), you'll get a 404 immediately.

2. Validate the Form Action in login.jsp

Ensure your form's action attribute aligns perfectly with the url-pattern in web.xml. To avoid context root issues, use a dynamic path that automatically picks up your app's context:

<form method="post" action="${pageContext.request.contextPath}/Login">
    <label>Username: <input type="text" name="username"></label>
    <label>Password: <input type="password" name="password"></label>
    <input type="submit" value="Submit">
</form>

Using ${pageContext.request.contextPath} eliminates hardcoding your app's root path, which is easy to mess up when deploying.

3. Confirm Servlet Class Placement

Your compiled LoginServlet.class file must live in the right place within your web app structure. If your servlet is in the com.yourpackage package, the path should be:
WEB-INF/classes/com/yourpackage/LoginServlet.class
If the class is missing from this location or the package structure is wrong, Tomcat can't find it, leading to a 404.

4. Check Tomcat Deployment & Context Root
  • Verify your web app folder (or WAR file) is properly deployed in Tomcat's webapps directory.
  • Make sure you're using the correct context root in your URL. For example, if your app folder is named login-app, the full URL to access the servlet should be http://localhost:8080/login-app/Login—omitting the context root will result in a 404.
5. Ensure Your Servlet is Implemented Correctly

Double-check that your LoginServlet properly extends HttpServlet and overrides the right method (doPost if your form uses method="post"):

package com.yourpackage;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // Your login logic here
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // ...
    }
}

If you forget to extend HttpServlet or use an incorrect method signature, Tomcat won't recognize it as a valid servlet.

Start with the first two checks—mapping and form action mismatches are the most frequent causes of this exact error. Once those are confirmed, work through the rest, and you should get past the 404.

内容的提问来源于stack exchange,提问作者Vinay Kumar Pabha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:34:26