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

查看:371
本文介绍了如何在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); 
    }
}

为什么仍然无法使用?我仍然需要在网址中键入/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的URL模式.您只需更改它即可从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映射网址中/和/*之间的差异模式.

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

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