Tomcat giving 404 error with servlets

2019-08-05 08:16发布

问题:

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 pc without any errors.

But after i hosted my application, I can't access servlets. It is giving me 404 error. I'm not used web.xml in my application to map servlets. I used annotation for it.

Hosted server use Tomcat 7 and Open JDK 1.7.

What could be the issue ? How can i solve this ?

New Update

The place where i have purchased hosting gave me cPanel to upload files. There is no place to upload war file or place to upload files in to webapps director in tomcat. so i uploaded files in to public_html directory. i think this may be the issue. because when i'm trying to access servlet like www.mysite.com/A , server search for this in root directoy. but acctually it is not there. so it gives 404 error.

I thnik this may be the issue. Any suggesions ?

UPDATED

Annotation is like below

@WebServlet(name = "AddCustomer", urlPatterns = {"/AddCustomer"})

Download Servlet from DropBox.

Directory structure.

回答1:

After deploying your project in to tomcat check in to "tomcat7\work\Catalina\localhost\your app" if the classes are present into the location. If classes are there then it should not be the issue.



回答2:

Annotations don't work "out-of-the-box", you still have to tell the Tomcat to look for annotated Servlet classes. And to do that you need to use the correct version of the 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 specify Dynamic Web Project facet to Servlet 3.0 value which gets translated to the web.xml line shown above.

You should check your project's WEB-INF folder for web.xml or try exporting it properly - a WAR file should (must?) contain WEB-INF/web.xml file.

UPDATE

You "tell it" by using Servlet 3.0 version in the web.xml header. For earlier versions Tomcat won't expect annotation-marked servlets, i.e. you would have to list them manually in the XML.

As for the updated problem - you are getting 404 because the Apache HTTP Server (probably that is what is installed) doesn't understand WAR or JSP files, you need to configure it to forward the requests to the Tomcat which is probably on another port (default is 8080) - this is known as AJP or HTTP proxy, i.e. Apache server is acting as a front-side proxy to Tomcat. There are manny answers on that subject here at Stackoverflow. You don't deploy a WAR file to public_html because that's where PHP or Perl webapps are deployed.

You should check with your hosting provider if you really have Tomcat installed, at which port is it and where to deploy your Java web application.



回答3:

check the following:

  • is the app below tomcat/webapps
  • can you see it from tomcat manager
  • any errors in the logs

Finally maybe post part of you web.xml and what url you are calling

Edit

So this is your code tree, but what has been deployed to your tomcat webapp directory? Are your compiled classes under WEB-INF?

Edit Do you new information received about the webapp being hosted in public_html I suggested you contact the resources of you hosted server provider to find out how to deploy correctly - maybe it is not even possible.

You could try getting a year long trial of EC2 from Amazon