基本的 Web 应用程序但出现 404 错误 Tomcat/Eclipse

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16579530/
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 20:30:12  来源:igfitidea点击:

Basic web application but getting 404 error Tomcat/Eclipse

javaeclipsejakarta-eetomcat

提问by Pradeep Simha

I have a simple dynamic web application in eclipse as shown in below image:

我在 Eclipse 中有一个简单的动态 Web 应用程序,如下图所示:

enter image description here

在此处输入图片说明

and my web.xml is as below (provided only relavent sections to reduce complexity):

我的 web.xml 如下(仅提供相关部分以降低复杂性):

<web-app ... 
<display-name>DemoRest</display-name>
<servlet>
   <servlet-name>JerseyWebService</servlet-name>
   <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>JerseyWebService</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
  <welcome-file>index.html</welcome-file>
</welcome-file-list>

But when I run I am getting "404 error" as shown below: enter image description here

但是当我运行时,我收到“404 错误”,如下所示: 在此处输入图片说明

As you can see my Context-Path matches with my folder and actual files are there in correct location, still I am not able to run the application. I tried of cleaning, rebuilding, restarting of eclipse but still no luck. Can anyone help me, why this happens with tomcat?

正如您所看到的,我的 Context-Path 与我的文件夹匹配,并且实际文件位于正确的位置,但我仍然无法运行该应用程序。我尝试了清理,重建,重新启动 eclipse 但仍然没有运气。谁能帮助我,为什么tomcat会发生这种情况?

回答by Himanshu Bhardwaj

<servlet-mapping>
   <servlet-name>JerseyWebService</servlet-name>
   <url-pattern>/*</url-pattern>
</servlet-mapping>

This is not available because the url

这不可用,因为网址

 http://localhost:8080/DemoRest/NewFile.jsp

will be intercepted by JerseyWebService, which I guess is not an intended action.

将被 JerseyWebService 拦截,我猜这不是预期的操作。

Why not host rest services on path like:

为什么不在路径上托管休息服务,例如:

<servlet-mapping>
   <servlet-name>JerseyWebService</servlet-name>
   <url-pattern>/rest/*</url-pattern>
</servlet-mapping>