When using Java for web development with Tomcat, you may encounter the HTTP 404 error shown below.
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:
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.
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
.
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.