Jus12
Jus12

Reputation: 18024

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

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

<?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.jsp is 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.xml correct?

(versions: jetty-8.0.y.z-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.

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

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

Upvotes: 6

Views: 15607

Answers (3)

jjoller
jjoller

Reputation: 681

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:

<!-- 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

Upvotes: 3

Justin Cuaresma
Justin Cuaresma

Reputation: 459

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

Upvotes: 0

user2948112
user2948112

Reputation:

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

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

Upvotes: 3

Related Questions