I've got some problem with understanding the purpose of the default servlet (the one mapped to the "/" url in DD).
I understand that this servlet will be served if no other web-component is matching the requested URL.
But what about welcome-files and static resources? How they can be accessed if I define the default servlet?
If I define it, I can still access the JSP files but the static ones (which does exist in the application and are accessible) are unavailable (their request is intercepted by the default servlet). The result is exactly the same when the static resource doesn't exist at all.
BTW: As I understand, the JSP files are working fine because they are web-component parts. But, talking low-level, are they dispatched to the JSP container or some other magic applies?
To access the static resources in your web-application the URL has to start with the context-path. Consequently, if the URL matches on the context-path, the URL mapping stategies of servlet spec 2.4 applies (SRV.11.1 Use of URL Paths)
The path used for mapping to a servlet is the request URL from the request
object minus the context path and the path parameters. The URL path mapping
rules below are used in order. The first successful match is used with no further
1. The container will try to find an exact match of the path of the request to the
path of the servlet. A successful match selects the servlet.
2. The container will recursively try to match the longest path-prefix. This is done
by stepping down the path tree a directory at a time, using the '/' character as
a path separator. The longest match determines the servlet selected.
3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet
container will try to match a servlet that handles requests for the extension.
An extension is defined as the part of the last segment after the last '.' character.
4. If neither of the previous three rules result in a servlet match, the container will
attempt to serve content appropriate for the resource requested. If a "default"
servlet is defined for the application, it will be used.
Let me try to give an example:
You have a web-application with a context root of AppName. To access a static resource, like a html file, in the war (directory: /fooStuff/foo.html) you will have to use a request URL like:
Your web.xml contains only the following entry:
The mapping of the URL (http://localhost:8080/AppName/fooStuff/foo.html) will follow the rules as defined above here and will end up in rule number 4. If you have defined a default servlet (and we have) the request will mapped to the default servlet (nl.errors.DefaultServlet).
Frits Walraven wrote:The mapping of the URL (http://localhost:8080/AppName/fooStuff/foo.html) will follow the rules as defined above here and will end up in rule number 4. If you have defined a default servlet (and we have) the request will mapped to the default servlet (nl.errors.DefaultServlet).
Hi Frits, Thanks for the detailed answer.
So having a default servlet doesn't seem like such a good idea. After all, when users type in http://localhost:8080/AppName/fooStuff/foo.html, we would like to send back foo.html, not execute some other default servlet. Ofcourse, there might be exceptions, depending on the design requirement. But in general default servlets are not a good idea, else, you cannot really have any static resources, right?