How to Fix Tomcat 404 Error

Sheeraz Gul Feb 02, 2024
  1. Tomcat 404 Error in Java
  2. Tomcat 404 Error in Eclipse
How to Fix Tomcat 404 Error

This tutorial demonstrates how to troubleshoot the Tomcat 404 error in Java.

Tomcat 404 Error in Java

While using the apache Tomcat server for web development in Java, the most common error is the HTTP Status 404. This error means the server cannot find the required resource.

The required file can be anything like HTML, Image Resource or JSP.

Most of the time, the error occurs when the required reference is not present or is referenced incorrectly. The error looks like this:

Tomcat 404 Error

There are three main reasons for this error in Apache Tomcat.

  1. The URL is Case Sensitive

    The Tomcat URLs are case sensitive, so whenever you are trying to write the URL by yourself, make sure that it is also correct in its case. Otherwise, it will throw the 404 status error.

  2. The Servlets Do Not Handle the URL

    The @Webservlet() is also used to handle the URL/demo, but when requesting the URL, it can be URL/this_demo, which is a different reference. This can be fixed by using the URL mapping and referencing the URL correctly.

    See example:

    @WebServlet("/demo")
    public class Demo extends HttpServlet {
      // Your code here.
    }
    

    Now, if the website requests the URL this_demo, we can solve it by changing the demo to this_demo in the URL mapping.

  3. Resource Forward by Servlets Does Not Exist

    When the resource forwarded by servlets does not exist, the Tomcat will throw a 404 error. Make sure the resource forwarded exists and the name of that resource is correct.

    For example, if we are referencing the DemoForm.jsp but the real name of that resource is Demo_Form.jsp, it will throw the 404 status error. We can change the DemoForm.jsp to Demo_Form.jsp to solve this error.

    See example:

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

Tomcat 404 Error in Eclipse

While working with Tomcat in Eclipse IDE, the same 404 error can occur even if we have managed all the solutions above. Sometimes, even if the Tomcat starts, the browser will throw the 404 error while working with Eclipse IDE.

This error is because Tomcat is not configured correctly in the Eclipse IDE. To solve this issue in Eclipse, follow the steps below.

  • Make sure Tomcat is downloaded and extracted.
  • Open the Eclipse IDE. Make sure you are using the EE version of Eclipse.
  • Go to the Servers tab in Eclipse, and if you see no Tomcat server, click create a new server. Or go to the Window menu, then Preferences and then Server and Add New.

    Tomcat Server Tab

    Tomcat Server Add

  • Select your version of Tomcat from the Apache folder on the page and click Next.

    Tomcat Server Add Apache

  • Click Browse and select your Tomcat directory. The installed JRE is okay if it works; otherwise, add the latest version. Click Next.

    Tomcat Server Add Path

  • Select your project and click Add and then Finish. A Tomcat server will be added to the Servers tab.

    Tomcat Server Added

  • Double click the Tomcat Server in the Servers tab, and a page will open. Under the Server Locations, select Use Tomcat Installation.
  • Save the configuration by Ctrl+S.
  • Restart the server, right-click on the server name in the Servers tab and click Restart.

    Tomcat Restart

  • Now the server works perfectly.

    Tomcat Started

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Error