[Solved] Tomcat Error HTTP Status 404 Not Found
- Details
- Written by Nam Ha Minh
- Last Updated on 05 November 2019   |   Print Email
In Java web development with Tomcat, it’s very often that you get HTTP 404 error like this:
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:
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:
- Java Servlet Quick Start for beginners (XML)
- Java Servlet for beginners (annotations)
- Java Servlet and JSP Hello World Tutorial with Eclipse, Maven and Apache Tomcat
- Handling HTML form data with Java Servlet
- Java File Download Servlet Example
Comments
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
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)
the url[/qu0
the url