Java Web应用编辑/删除功能失效,点击后报HTTP 404错误
Hey there, let's tackle that 404 error you're hitting when clicking edit/delete links—this is one of the most common hiccups when setting up CRUD workflows in Java Web apps, so we'll break down the most likely fixes step by step.
First, Check Your Link Paths (The #1 Culprit)
Your JSP snippet cuts off before the edit/delete links, but odds are the issue starts here. When you use <a> tags for edit/delete, the href path has to match exactly with your servlet's mapped URL, including your app's context path.
Common Mistakes & Fixes:
- Missing context path: If your app is deployed with a context path (e.g.,
http://localhost:8080/your-crud-app), hardcoding paths likehref="/edit"will send requests tohttp://localhost:8080/edit(the server root) instead of your app. Fix this by using the dynamic context path variable:<a href="${pageContext.request.contextPath}/edit?id=${user.id}">Edit</a> <a href="${pageContext.request.contextPath}/delete?id=${user.id}">Delete</a> - Mismatched path segments: If your servlet is mapped to
/users/edit, your link must use that exact path, not/editor/user/edit. Double-check thehrefagainst your servlet's mapping. - Missing ID parameter: Even if the path is right, forgetting to pass the record ID (like
?id=${user.id}) won't cause a 404, but it will break your edit/delete logic later—so make sure that's included too.
Verify Your Servlet Mappings
Next, confirm that your edit and delete servlets are mapped to the exact paths you're using in your links. There are two ways to do this:
Annotation-Based Mapping (Servlet 3.0+)
Check your servlet classes for @WebServlet annotations. They should match the paths in your links:
// Edit Servlet @WebServlet("/edit") public class EditUserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Fetch the user by ID, forward to edit page int userId = Integer.parseInt(req.getParameter("id")); // ... your DB logic here req.getRequestDispatcher("/edit-user.jsp").forward(req, resp); } } // Delete Servlet @WebServlet("/delete") public class DeleteUserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int userId = Integer.parseInt(req.getParameter("id")); // ... execute delete logic // Redirect back to the list page after deletion resp.sendRedirect(req.getContextPath() + "/list-users"); } }
XML-Based Mapping (web.xml)
If you're using web.xml instead of annotations, make sure your <servlet-mapping> entries match your link paths:
<servlet> <servlet-name>EditUserServlet</servlet-name> <servlet-class>com.yourpackage.EditUserServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>EditUserServlet</servlet-name> <url-pattern>/edit</url-pattern> </servlet-mapping> <servlet> <servlet-name>DeleteUserServlet</servlet-name> <servlet-class>com.yourpackage.DeleteUserServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DeleteUserServlet</servlet-name> <url-pattern>/delete</url-pattern> </servlet-mapping>
Quick Debugging Trick
When you click the edit/delete link, look at your browser's address bar. The URL should match exactly what your servlet is mapped to. For example:
- If your app's context path is
crud-app, the edit URL should behttp://localhost:8080/crud-app/edit?id=1 - If it shows
http://localhost:8080/edit?id=1(missing the context path), that's your problem—fix the link to include${pageContext.request.contextPath}.
Bonus: Check Request Methods (If You Get 405 Later)
A 404 means the resource isn't found, but if you fix the path and get a 405 (Method Not Allowed), that means your servlet is expecting a different request method (e.g., POST for delete) but your link uses GET. For delete, it's best practice to use POST, but if you're starting out, using GET is okay for testing—just make sure your servlet's doGet method is implemented.
内容的提问来源于stack exchange,提问作者Rasel

