如何在 web.xml 中配置欢迎文件列表 [英] How to configure welcome file list in web.xml

查看:37
本文介绍了如何在 web.xml 中配置欢迎文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 web.xml 文档中有这个.我正在尝试使用欢迎列表,因此我不再需要键入主页的路径.但是每次在我的 tomcat 页面中单击该应用程序时,它都会显示请求的资源不可用.

I have this in my web.xml document. I am trying to have a welcome list so I dont need to type the path for the home page anymore. But everytime a clicked the application in my tomcat page it displays The requested resource is not available.

<listener>
    <listener-class>web.Init</listener-class>
</listener>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>index</servlet-name>
    <servlet-class>web.IndexServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>index</servlet-name>
    <url-pattern>/index</url-pattern>
</servlet-mapping>

我的jsp页面的servlet

My servlet for the jsp page

package web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class IndexServlet extends HttpServlet
{
    private Logger logger = Logger.getLogger(this.getClass());
    private RequestDispatcher jsp;

    public void init(ServletConfig config) throws ServletException
    {
        ServletContext context = config.getServletContext();
        jsp = context.getRequestDispatcher("/WEB-INF/jsp/index.jsp");
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        logger.debug("doGet()");
        jsp.forward(req, resp); 
    }
}

为什么它仍然不起作用?我仍然需要在我的 url 中输入/index...如何正确地做到这一点?

Why is that it is still not working?I still need to type the /index in my url...How to do this correctly?

推荐答案

你需要把JSP文件放在/index.jsp而不是/WEB-INF/jsp/index.jsp.顺便说一下,这样整个 servlet 是多余的.

You need to put the JSP file in /index.jsp instead of in /WEB-INF/jsp/index.jsp. This way the whole servlet is superflous by the way.

WebContent
 |-- META-INF
 |-- WEB-INF
 |    `-- web.xml
 `-- index.jsp

如果您绝对肯定需要以这种奇怪的方式调用 servlet,那么您应该将它映射到 /index.jsp 而不是 /index.您只需要更改它以从 request 而不是从 config 获取请求调度程序并摆脱整个 init() 方法.

If you're absolutely positive that you need to invoke a servlet this strange way, then you should map it on an URL pattern of /index.jsp instead of /index. You only need to change it to get the request dispatcher from request instead of from config and get rid of the whole init() method.

如果您真的打算拥有一个主页 servlet"(因此不是欢迎文件—它具有完全不同的目的;即当folder 被请求,因此它不是特定的根文件夹),那么您应该将 servlet 映射到空字符串 URL 模式.

In case you actually intend to have a "home page servlet" (and thus not a welcome file — which has an entirely different purpose; namely the default file which sould be served when a folder is being requested, which is thus not specifically the root folder), then you should be mapping the servlet on the empty string URL pattern.

<servlet-mapping>
    <servlet-name>index</servlet-name>
    <url-pattern></url-pattern>
</servlet-mapping>

另见servlet映射url中/和/*的区别模式.

这篇关于如何在 web.xml 中配置欢迎文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆