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:
There are three main reasons for this error in Apache Tomcat.
-
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.
-
The Servlets Do Not Handle the URL
The
@Webservlet()
is also used to handle theURL/demo
, but when requesting the URL, it can beURL/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 tothis_demo
in the URL mapping. -
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 isDemo_Form.jsp
, it will throw the 404 status error. We can change theDemoForm.jsp
toDemo_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, clickcreate a new server
. Or go to theWindow
menu, thenPreferences
and thenServer
andAdd New
. -
Select your version of Tomcat from the Apache folder on the page and click
Next
. -
Click
Browse
and select your Tomcat directory. The installed JRE is okay if it works; otherwise, add the latest version. ClickNext
. -
Select your project and click
Add
and thenFinish
. A Tomcat server will be added to theServers
tab. -
Double click the Tomcat Server in the
Servers
tab, and a page will open. Under theServer Locations
, selectUse Tomcat Installation
. -
Save the configuration by Ctrl+S.
-
Restart the server, right-click on the server name in the
Servers
tab and clickRestart
. -
Now the server works perfectly.
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 FacebookRelated Article - Java Error
- How to Fix the Error: Failed to Create the Java Virtual Machine
- How to Fix the Missing Server JVM Error in Java
- How to Fix the 'No Java Virtual Machine Was Found' Error in Eclipse
- How to Fix Javax.Net.SSL.SSLHandShakeException: Remote Host Closed Connection During Handshake
- How to Fix the Error: Failed to Create the Java Virtual Machine
- How to Fix Java.Lang.VerifyError: Bad Type on Operand Stack