Java Web 应用程序:欢迎文件列表无法使用 servlet 映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20001761/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:15:29  来源:igfitidea点击:

Web-app: welcome-file-list not working using servlet-mapping

javajspservletsweb.xml

提问by Jus12

When visiting the bare url (e.g., localhost:8080), I want the same behavior as visiting localhost:8080/foo. A servlet (actually a JSP) is mapped to /foo. My web.xml is

当访问裸 url(例如localhost:8080)时,我想要与访问相同的行为localhost:8080/foo。一个 servlet(实际上是一个 JSP)被映射到/foo. 我的 web.xml 是

<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <display-name>Test</display-name>
    <description></description>

    <!-- servlets -->

    <servlet>
        <servlet-name>TestServlet</servlet-name>
        <jsp-file>/test/welcome.jsp</jsp-file>
    </servlet> 

    <!-- mappings -->
    <servlet-mapping> 
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/foo</url-pattern>
        <url-pattern>/foo/*</url-pattern>
    </servlet-mapping> 

    <welcome-file-list>
        <welcome-file>/foo</welcome-file>
    </welcome-file-list> 
</web-app> 

When I do http://localhost:8080/foo, I get the correct output; the output of test/welcome.jspis displayed. However, when visiting the bare url http://localhost:8080, jetty displays the file list and Tomcat 6 gives a page not found. Is my web.xmlcorrect?

当我这样做时http://localhost:8080/foo,我得到了正确的输出;test/welcome.jsp显示输出。但是,在访问裸 url 时http://localhost:8080,jetty 显示文件列表,而 Tomcat 6 给出一个页面未找到。我的web.xml正确吗?

(versions: jetty-8.0.y.z-SNAPSHOT, Tomcat 6, Java 1.6, servlet-api-2.5)

(版本:jetty-8.0.yz-SNAPSHOT、Tomcat 6、Java 1.6、servlet-api-2.5)

Changing <welcome-file>/foo</welcome-file>to <welcome-file>foo</welcome-file>makes no difference.

更改<welcome-file>/foo</welcome-file><welcome-file>foo</welcome-file>没有区别。

Is there a mistake in my web.xml? If not, what is the right way to do what I want.

我的 web.xml 中是否有错误?如果没有,做我想做的事情的正确方法是什么。

EDIT: Seems to be a bug in Jetty-8. It started working in Jetty-9.

编辑:似乎是 Jetty-8 中的一个错误。它开始在 Jetty-9 中工作。

回答by Jus12

remove /from this <welcome-file>/foo</welcome-file>It should be

/从中 删除<welcome-file>/foo</welcome-file>它应该是

<welcome-file>foo</welcome-file>

回答by Justin Cuaresma

It should be:

它应该是:

<servlet-mapping> 
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/foo</url-pattern>
        <url-pattern>/foo/*</url-pattern>
</servlet-mapping> 

<welcome-file-list>
        <welcome-file>foo</welcome-file>
</welcome-file-list> 

Here's a good explanation: http://wiki.metawerx.net/wiki/HowToUseAServletAsYourMainWebPage

这是一个很好的解释:http: //wiki.metawerx.net/wiki/HowToUseAServletAsYourMainWebPage

回答by jjoller

Not sure if this also works in Jetty 8 (you should probably update anyway), but in Jetty 9 you can put the following code in your web.xml to enable Servlets as welcome files for Jetty:

不确定这是否也适用于 Jetty 8(您可能无论如何都应该更新),但在 Jetty 9 中,您可以将以下代码放入您的 web.xml 以启用 Servlet 作为 Jetty 的欢迎文件:

<!-- Enable servlets as welcome files -->
<context-param>
    <param-name>
       org.eclipse.jetty.servlet.Default.welcomeServlets
    </param-name>
    <param-value>true</param-value>
</context-param>

See this docs: http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/servlet/DefaultServlet.html

请参阅此文档:http: //download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/servlet/DefaultServlet.html