Eclipse, tomcat, 404 error

12,895

Solution 1

The tutorial is suggesting you to invoke it by http://localhost:8080/de.vogella.wtp.filecounter/FileCounter. The project name defaults to context name de.vogella.wtp.filecounter which you've changed to SampleServlet, so you need to invoke the servlet by http://localhost:8080/SampleServlet/FileCounter.

See also:


As to the SetPropertiesRule warning, just ignore it, that's normal. Eclipse is just adding an extra attribute to Tomcat's <Context> element to be able to associate the deployed webapp with a particular project. Tomcat is just jerking because it don't recognize it as one of the predefined <Context> attributes. It's however trying to be helpful for the case the enduser actually made a typo and so on. Just ignore it. You won't see it when you export the webapp and deploy it on a real production server.

Solution 2

Okay according to your web.xml it seems like you're missing a servlet definition and a servlet-mapping. I Don't know why this is not generated by your ide. It should be something like this:

<servlet>
    <servlet-name>SampleServlet</servlet-name>
    <servlet-class>your.package.SampleServlet</servlet-class> <!-- The full qualified package path to your Servlet class -->        
</servlet>

<servlet-mapping> 
    <servlet-name>SampleServlet</servlet-name>
    <url-pattern>/mysample</url-pattern>
</servlet-mapping>

In the servlet-mapping Element you just map any url to your servlet defined above. So if you now call http://yourserver:8080/projectname/mysample the Servlet your.package.SampleServlet will be called.

I hope that helps.

Share:
12,895
bancer
Author by

bancer

Careers profile: http://careers.stackoverflow.com/bancer Author of SprintDict for Android: http://sprintdict.bancer.net/

Updated on June 26, 2022

Comments

  • bancer
    bancer almost 2 years

    I am learning servlets and follow this tutorial (I follow step by step but I named the project "SampleServlet" instead of "de.vogella.wtp.filecounter"). When I start the server (step 5.4) I get 404 page error:

    HTTP Status 404 - /SampleServlet/servlet/de.vogella.wtp.filecounter.servlets.FileCounter
    type Status report
    message /SampleServlet/servlet/de.vogella.wtp.filecounter.servlets.FileCounter
    description The requested resource (/SampleServlet/servlet/de.vogella.wtp.filecounter.servlets.FileCounter) is not available.
    

    Where to start debugging? There were several "INFO" in the console when server started and one warning:

    29.08.2011 21:03:44 org.apache.tomcat.util.digester.SetPropertiesRule begin
    WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SampleServlet' did not find a matching property.
    

    Do I need to change any preferences?

  • BalusC
    BalusC over 12 years
    Please note that the OP posted the web.xml of Tomcat itself, not of the webapp.
  • bancer
    bancer over 12 years
    When I right click on the FileCounter.java file in the Project Explorer tab and select "Run As"->"Run on Server" the internal browser opens localhost:8080/SampleServlet/servlet/… with 404 error. localhost:8080/SampleServlet/FileCounter results with the same error.
  • bancer
    bancer over 12 years
    Super! That works! I wonder why these changes in web.xml file were not generated by Eclipse.
  • BalusC
    BalusC over 12 years
    Please note that you should not edit the web.xml of the server! You should edit or create the one of your webapp.
  • BalusC
    BalusC over 12 years
    I now understand why you run into problems. That tutorial is targeted on Servlet 2.5 while you've created a Servlet 3.0 project which by default get built without web.xml. You would need the @WebServlet annotation instead. Put this line @WebServlet(urlPatterns={"/FileCounter"}) on top of the servlet class declaration. And please, undo all changes you made in Tomcat's own web.xml. This is not the right way to declare your own servlets, it makes your webapp unportable!
  • Chris
    Chris over 12 years
    Yes BalusC is right! Look for the web.xml file located in your project directory under WEB-INF/web.xml.
  • boettger1
    boettger1 about 12 years
    @BalusC's answer brought me back from the brink of suicide. When creating the servlet from WebContent, simply don't overwrite the auto-generated @WebServlet("/FileCounter") annotation in the servlet class. What I'm still puzzled by is why Eclipse (Indigo) is not appending /FileCounter to the URL... why do we have to add it manually?