Eclipse、tomcat、404错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7240039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:55:55  来源:igfitidea点击:

Eclipse, tomcat, 404 error

javaeclipsetomcatservletshttp-status-code-404

提问by bancer

I am learning servlets and follow thistutorial (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:

我正在学习 servlets 并遵循教程(我一步一步地遵循,但我将项目命名为“SampleServlet”而不是“de.vogella.wtp.filecounter”)。当我启动服务器(步骤 5.4)时,我收到 404 页面错误:

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:

从哪里开始调试?服务器启动时控制台中有几个“INFO”和一个警告:

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?

我需要更改任何偏好吗?

回答by BalusC

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.filecounterwhich you've changed to SampleServlet, so you need to invoke the servlet by http://localhost:8080/SampleServlet/FileCounter.

本教程建议您通过http://localhost:8080/de.vogella.wtp.filecounter/FileCounter调用它。项目名称默认为de.vogella.wtp.filecounter您已更改为的上下文名称SampleServlet,因此您需要通过http://localhost:8080/SampleServlet/FileCounter调用 servlet 。

See also:

也可以看看:



As to the SetPropertiesRulewarning, 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.

至于SetPropertiesRule警告,忽略它,这是正常的。Eclipse 只是向 Tomcat 的<Context>元素添加了一个额外的属性,以便能够将部署的 web 应用程序与特定项目相关联。Tomcat 只是在抽搐,因为它没有将其识别为预定义的<Context>属性之一。然而,它试图对最终用户实际打错字等情况有所帮助。忽略它。当您导出 webapp 并将其部署在真实的生产服务器上时,您将看不到它。

回答by Chris

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:

好的,根据您的 web.xml,您似乎缺少 servlet 定义和 servlet 映射。我不知道为什么这不是由您的 ide 生成的。它应该是这样的:

<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-mappingElement you just map any url to your servlet defined above. So if you now call http://yourserver:8080/projectname/mysamplethe Servlet your.package.SampleServlet will be called.

servlet-mappingElement 中,您只需将任何 url 映射到上面定义的 servlet。因此,如果您现在调用http://yourserver:8080/projectname/mysampleServlet your.package.SampleServlet 将被调用。

I hope that helps.

我希望这有帮助。

回答by vovantam

Add FileCounter as one of a welcome-file in web.xml, it's look like below.

将 FileCounter 添加为 web.xml 中的欢迎文件之一,如下所示。

<?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>