点击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:
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.
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.
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.
- Verify your web app folder (or WAR file) is properly deployed in Tomcat's
webappsdirectory. - 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 behttp://localhost:8080/login-app/Login—omitting the context root will result in a 404.
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

