使用 eclipse 和 tomcat 7 运行和部署 servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11491398/
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
running and deploying servlet with eclipse and tomcat 7
提问by Cel
- I created a test project based on Tomcat HelloWorld Servlet with Eclipse
- and tried to run it from Eclipse as is with Tomcat 7, which I have configured to run on 127.0.0.1 - but I get
Page cannot be found
at 127.0.0.1/helloworld/HelloWorld - I also tried exporting as war file and deploying it to the (otherwise working) Tomcat server running as a Windows service - and deployed with the Tomcat Application Manager - manifest.mf and the classes are nicely copied to tomcat/webapps/helloworld, but trying to navigate to 127.0.0.1/helloworld/HelloWorld fails again, showing
HTTP Status 404
- 我用Eclipse创建了一个基于Tomcat HelloWorld Servlet的测试项目
- 并尝试像使用 Tomcat 7 一样从 Eclipse 运行它,我已将其配置为在 127.0.0.1 上运行 - 但我得到
Page cannot be found
了 127.0.0.1/helloworld/HelloWorld - 我还尝试将其导出为 war 文件并将其部署到作为 Windows 服务运行的(以其他方式工作的)Tomcat 服务器 - 并使用 Tomcat 应用程序管理器进行部署 - manifest.mf 并且这些类被很好地复制到 tomcat/webapps/helloworld,但正在尝试导航到 127.0.0.1/helloworld/HelloWorld 再次失败,显示
HTTP Status 404
From default @WebServlet to web.xml configuration
从默认的@WebServlet 到 web.xml 配置
Next, in HelloWorld.java, I tried commenting out
接下来,在 HelloWorld.java 中,我尝试注释掉
//@WebServlet("/HelloWorld")
and then adding a web-application-specific web.xml configuration:
然后添加一个特定于 Web 应用程序的 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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
But the results are still the same - no response!
但结果还是一样——没有反应!
Solutions?
解决方案?
- Given the linked tutorial provides instructions for Tomcat 6, should I change something to make it work with Tomcat 7?
- Specifically, is the default
@WebServlet("/HelloWorld")
added by Eclipse sufficient? What is needed for the annotation-based configuration of Servlet 3.0 to work (without web.xml)? - Or could it be that something is blocking any web app deployment at the global tomcat server level? I have changed the server configuration somewhat, and unfortunately i do not remember exactly what, except for making it serve on 127.0.0.1 rather than 127.0.0.1:8080
- 鉴于链接教程提供了 Tomcat 6 的说明,我是否应该更改某些内容以使其适用于 Tomcat 7?
- 具体来说,
@WebServlet("/HelloWorld")
Eclipse 添加的默认值是否足够?Servlet 3.0 基于注解的配置需要什么才能工作(没有 web.xml)? - 或者是否有什么东西阻止了全局 tomcat 服务器级别的任何 Web 应用程序部署?我稍微改变了服务器配置,不幸的是我不记得到底是什么,除了让它在 127.0.0.1 而不是 127.0.0.1:8080 上提供服务
回答by Arne
Open the properties page of your dynamic web project, click on "web project settings" and use the context root paramter as the root path for your URL.
打开动态 Web 项目的属性页面,单击“Web 项目设置”并使用上下文根参数作为 URL 的根路径。
回答by Siddhartha Kumar
Try putting a default Index.html page in the WebContent and see if the welcome page points to it. If yes then try entering the servlet name manually and see if that works. I get the same error but doing so fixes it.
尝试在 WebContent 中放置一个默认的 Index.html 页面,看看欢迎页面是否指向它。如果是,则尝试手动输入 servlet 名称,看看是否有效。我遇到了同样的错误,但这样做可以修复它。
http://localhost:8080/<Project_Name>/index.html
http://localhost:8080/<Project_Name>/servlet_name