【发布时间】:2013-12-08 15:59:49
【问题描述】:
我使用 Eclipse Java EE,我有 tomcat 7.xx 服务器和一个 Java Servlet。我需要在 Java Servlet 中指定起始页“WebContent/mypage.html”。
如何使用@WebServlet 注释做到这一点?
这是我的 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<welcome-file-list>
<welcome-file>/ricerca.htm</welcome-file>
</welcome-file-list>
<display-name>Searcher</display-name>
<description>
Searcher!
</description>
<servlet>
<servlet-name>Searcher</servlet-name>
<servlet-class>org.irlab.Searcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Searcher</servlet-name>
<url-pattern>/Searcher</url-pattern>
</servlet-mapping>
</web-app>
我的上下文根是:/localhost:8181/Searcher3/
好的一种解决方案是:@WebServlet(urlPatterns = {"/Searcher"}) dnd 在我编写的 doGet 方法中: request.getRequestDispatcher("ricerca.htm").forward(request,response);
但是为什么 web.xml 不起作用?我需要在 Eclipse 项目配置中添加一些东西吗?
【问题讨论】:
-
您的 web.xml 不起作用,因为它的格式不正确。查看一些模板here。不要使用自定义
@WebServlet重新发明轮子。使用已经存在的实用程序。 -
我已经更新了 web.xml 但还是不行..
-
请用您所做的更新编辑您的问题。
-
是的,我现在刚刚编辑,没有 @WebServlet 我有一个 404 错误。
-
您的文件名为
ricerca.htm或ricerca.html或mypage.html?使用正确的名称。