【问题标题】:Java Servlet: Specify start page wih @WebServlet annotationJava Servlet:使用 @WebServlet 注释指定起始页
【发布时间】: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.htmricerca.htmlmypage.html?使用正确的名称。

标签: xml servlets


【解决方案1】:

如何使用@WebServlet 注释来做到这一点?

index.html 是默认欢迎页面之一。您可以使用 @WebServlet 注释将 Servlet 称为欢迎页面。您需要将您的 Servlet urlPatterns 映射为 /index.html。与doGet(..) 方法相比,您可以使用RequestDispatcher.forward(..) 转发到mypage.html

 @WebServlet(urlPatterns = {"/index.html"})
 public class IndexServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)

        throws ServletException, IOException {
       // forward to mypage.html
       request.getRequestDispatcher("mypage.html").forward(request,response);
    }
 }

【讨论】:

  • @Neptune,对于 Java EE 6,web.xml 是可选的。你不需要 web.xml。
  • 很好,这行得通。但是为什么我不能将“mypage.tm”直接放在 urlPatterns 中?
  • @Neptune,你可以这样做,而不是你的 URL 应该像:http://localhost:8080/AppName/mypage.html。如果你像index.html 那样映射,那么http://localhost:8080/AppName/ 就足够了。
  • 现在我有一个问题,我可以找到“mypage.html”但我找不到 MyApp。如果我去:localhost:8181/Searcher3 我看 mypage.htm 但如果去:localhost:8181/Searcher3/Searcher 我有 404 错误,
  • 好的解决方案是这样的:@WebServlet(urlPatterns = {"/Searcher"}) 在 doGet 方法中我写 request.getRequestDispatcher("ricerca.htm").forward(request,response) ;但是为什么 web.xml 不起作用?
【解决方案2】:

您不需要为此自定义 Servlet。只需添加

<welcome-file-list>
    <welcome-file>mypage.html</welcome-file>
</welcome-file-list>

元素到您的web.xmlThis is documented here.

【讨论】:

  • 我已经这样做了,但是没有用。我已经在 WebContent/WEB-INF 文件夹中添加了 web.xml,然后我运行了 Eclipse 项目但没有工作。
  • @Neptune 你说的不工作是什么意思?你有404吗?应用程序没有启动?你提出了什么样的要求,你希望工作?
  • 是的,我得到一个“HTTP 状态 404”,不加载 html 页面。
  • 我认为您得到 404 的原因是您使用 *.html 进行 servlet 映射,因此当您请求 mypage.html 时,servlet 容器找不到该映射的任何 servlet,因此它返回 404。使用解决方案@Masud 的。
  • @Neptune 如果您有welcome-file 声明并且您的文件在webcontent/mypage.html 中并尝试访问your-host:your-port/your-context-root,则应该加载该页面。您可以使用完整的web.xml 和上下文根来编辑您的问题吗?
猜你喜欢
  • 2021-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
  • 2013-01-30
  • 2011-11-13
  • 2011-09-26
  • 2013-12-09
相关资源
最近更新 更多