Troubleshooting Tomcat error 404 in Java

When using Java for web development with Tomcat, you may encounter the HTTP 404 error shown below.

widget

This error indicates that the server could not find the desired resource. This resource can be any file such as JSP, HTML, or image resource. Usually, the resource is present, but it is referenced incorrectly. In most cases, you can fix this by correcting the URL. Here are three strategies you can use to look for errors:

  1. Java servlets do not handle URL
  2. Servlet forwarding the resource does not exist
  3. URL is case-sensitive

1. Java servlets do not handle URL

Your @Webservlet() may handle for URL/name; however, the URL requested may be URL/this_name (different reference). You can fix this by correcting the reference URL or URL mapping.

In code, it may look something like this:

@WebServlet("/name")
public class Name extends HttpServlet {
...
}

However, your website requested /this_name instead of /name. One way you can correct this is by changing /name to /this_name in your URL mapping.

2. Servlet forwarding the resource does not exist

Make sure that the forwarded resource exists. If the resource you are trying to reference is not named correctly, you may also run into this problem. For instance, you are referencing signupForm.jsp, but the name of the resource is signup_Form.jsp. In code it may look something like this:

String signupForm= "frontend/signupForm.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(signupForm);
dispatcher.forward(request, response);

You can fix this by correcting the servlet’s path which, in this case, would be to change signupForm.jsp to signup_Form.jsp.

3. URL is case-sensitive

If you typed the URL yourself, you might have mistyped it. Tomcat URLs are case-sensitive. For instance, signup is different than signUp. Make sure your URL is case-sensitive.

Copyright ©2024 Educative, Inc. All rights reserved