0

The following web.xml doesn't seem to work:

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>*.wfn</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>SelectSessionServer.wfn</welcome-file>
</welcome-file-list>

Instead, a directory listing is shown. I have an index.html file in there, and that index.html file has a link to exactly the same SelectSessionServer URL, and then it works properly.

("it" = Glassfish & Tomcat)

How come ?

I've seen this question ( servlet as welcome-file-list in tomcat 7 ) and its responses seem to suggest that my setup should be working.

Community
  • 1
  • 1
Erwin Smout
  • 18,113
  • 4
  • 33
  • 52
  • 1
    Should work just fine. How are you deploying? That the directory listing is been shown is also odd; they are by default turned off, so they should have been purposefully turned on. Who's managing the containers and deploying the webapps? You or some server admin? – BalusC Nov 06 '11 at 14:29
  • Only Glassfish actually displays a dirlist. Tomcat gives a -404 (or whatever the "unavailable" error is), but that's past the point. I know that's just a config option even if I don't know where it is. The point, why doesn't it work as I expect, and as it apparently should, according to what info I found here. I'll try and take a look in the appserver's logs and see what's there. – Erwin Smout Nov 06 '11 at 15:20
  • Deploying is using a WAR file. But it shouldn't related since I get this behaviour also after the appserver is stopped/started. – Erwin Smout Nov 06 '11 at 15:24
  • I figured it out. There must be a file of the very same name. Otherwise, the AS sees the "welcome-file" name, finds that it is not a file, and won't bother to "submit" it as a URL to process. The contents are immaterial and won't be used, but the file must exist. I doubt if this behaviour is specced as such, but it fixes the problem in both Glassfish and Tomcat. – Erwin Smout Nov 06 '11 at 22:04
  • Thus, your `web.xml` was declared as Servlet 2.5 instead of Servlet 3.0 even though you target Servlet 3.0 compatible containers? – BalusC Nov 06 '11 at 22:15

1 Answers1

2

Make sure that your web.xml root declaration conforms Servlet 3.0 in order to map a servlet on a welcome file. You're using Tomcat 7 and (supposedly) Glassfish 3, which are Servlet 3.0 containers, so you should make sure that your web.xml is also declared as such.

<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

It'll also give additional benefits of new Servlet 3.0 features such as @WebServlet annotation and so on.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555