In Java web development with Tomcat, it’s very often that you get HTTP 404 error like this:

tomcat-error-404

The error code is HTTP 404 (not found) and the description is:

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

This error means the server could not find the requested resource (JSP, HTML, images…) and returns HTTP status code 404. Most of the time, you can fix this error by correcting the URL. However, sometimes it’s not easy like that, making it is an annoying error.

Here I suggest some possible reasons and how to fix the error HTTP 404 in Java web development with Tomcat.

 

1. The URL is not handled by any Java servlets

You need to check URL mapping in your servlet classes to make sure the requested URL is actually handled by a servlet. For example:

@WebServlet("/view_book")
public class ViewBookServlet extends HttpServlet {
...
}
This servlet handles the URL /view_book. If the request URL is /view_books the server will raise HTTP 404 error. You can fix by either correcting the URL or correcting the URL mapping in the @WebServlet annotation.



In older Java web application, you have to check the web deployment descriptor file web.xml because a Java servlet can be mapped to URL via XML like this:

<servlet-mapping>
    <servlet-name>ViewBookServlet</servlet-name>
    <url-pattern>/view_book</url-pattern>
</servlet-mapping>
 

2. Java servlet forwarding to a resource that does not exist

In this case, the requested URL is handled by a Java servlet, but code in the servlet forwards to a resource (JSP, HTML…) which does not exist, as shown in the following screenshot:

tomcat-error-404-forwarding

The code in the servlet class would look like this:

String registerForm = "frontend/registerform.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(registerForm);
dispatcher.forward(request, response);
You can fix by correcting the forward path in the servlet, and make sure that the forwarded resource does actually exist in the given path.

 

3. URL is case-sensitive

Note that Tomcat treats URL as case-sensitive, for instance /Register is different than /register. So you need to check and use correct case for the letters in request URL.

Also pay attention to the webapp name in the URL, for instance http://localhost:8080/BookstoreWebsite/ is different than http://localhost:8080/BookStoreWebsite/

TIP: in Eclipse, you can right click on the project, then click Run As > Run on Server, the IDE will always use the correct name of the web application.

Finally, you should not let the user see the raw HTTP 404 error page rendered by the server. Instead, you should design your own user-friendly 404 error page – follow this tutorial: How to Handle Error for Java web applications.

You can also watch the video version below:

 

Other Java Servlet Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#35ISHITA BARANWAL2024-04-17 06:50
Hi! My Servlet project was running fine until now, but it suddenly started giving Error 404: Type Status Report

Message The requested resource [/web-app/second] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.


I am using Java 17 with Tomcat 9
Quote
#34Emon2023-12-04 18:07
HTTP Status 404 – Not Found
Type Status Report

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/9.0.31 (Ubuntu)
Quote
#33Rajamohan Reddy2023-11-02 02:25
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Quote
#32Siva2023-06-01 01:54
[quote name="selami Ozlu"]yeap got the same .
the url[/qu0
Quote
#31selami Ozlu2023-04-06 09:21
yeap got the same .
the url
Quote