Eclipse, tomcat, 404 error
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.je开发者_如何学JAVAe.server:SampleServlet' did not find a matching property.
Do I need to change any preferences?
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:
- Our Servlets wiki page
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.
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.
Add FileCounter as one of a welcome-file in web.xml, it's look like below.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>de.vogella.wtp.filecounter</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<!-- <welcome-file>FirstJSP.jsp</welcome-file> -->
<welcome-file>FileCounter</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>FileCounter</display-name>
<servlet-name>FileCounter</servlet-name>
<servlet-class>de.vogella.wtp.filecounter.servlets.FileCounter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileCounter</servlet-name>
<url-pattern>/FileCounter</url-pattern>
</servlet-mapping>
</web-app>
精彩评论