Tomcat gives 404 error with servlets
I have developed a Java web application using Tomcat 7
and Oracle JDK 1.7
with NeatBeans 7.3
. My application runs on my local machine without any error.
But after hosting my application, I cannot access the servlets. This is giving me an error 404
. I am not using web.xml
Servlet Mapping in my application. I used annotation
to do this.
Using server hosting Tomcat 7
and Open JDK 1.7
.
What could be the problem? How can I solve this?
New update
The place where I bought the hosting gave me cPanel
to download files. No file upload war
space, or no file upload space in directory webapps
in tomcat. so I downloaded the files to a directory public_html
. I think this might be a problem. because when I try to access servlets for example the www.mysite.com/A
server looks for this in root
directoy. but in fact it is not there. so it gives an error 404
.
This could be a problem. Any suggestions?
UPDATED
Notation below
@WebServlet(name = "AddCustomer", urlPatterns = {"/AddCustomer"})
Download the servlet from DropBox .
Directory structure.
source to share
check the following:
- is the app below tomcat / webapps
- You can see this from the tomcat manager
- any errors in the logs
Finally, maybe post part of your web.xml and url you are calling
Edit
So this is your code tree, but what has been deployed in your tomapp catapp directory? Are your compiled classes under WEB-INF?
Edit Are you getting new information about the website being hosted in public_html. I suggested that you contact your hosting provider resources to find out how to deploy correctly - it might not even be possible.
You can try Amazon's EC2 Long Trial
source to share
Annotations don't work out of the box, you still need tell
Tomcat to look for annotated classes Servlet
. And for that you need to use the correct version web.xml
:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
I haven't used NetBeans, but like Eclipse it probably has project properties that let you set the face of a dynamic web project to Servlet 3.0 with a value that translates into the string web.xml
shown above.
You should check your project folder WEB-INF
for web.xml
or try to export it correctly - the WAR file should (should?) Contain WEB-INF/web.xml
a.
UPDATE
"Tell me about it" using Servlet 3.0 in the title web.xml
. For earlier versions, Tomcat does not expect annotated servlets, i.e. You will need to manually enumerate them in XML.
Regarding the updated issue - you get a 404 because the Apache HTTP server (maybe that's what is installed) doesn't understand WAR or JSP files, you need to configure it to forward requests to Tomcat, which is probably on a different port (by default 8080) - This is known as AJP or HTTP Proxy , meaning the Apache server acts as a third party proxy for Tomcat. There are random answers to this question here on Stackoverflow. You are not deploying the WAR file to public_html
, because that's where PHP or Perl web applications are deployed.
You should check with your hosting provider if you actually have Tomcat installed, what port it is on, and where to deploy your Java web application.
source to share