6

Related to this question, is the idea of a default servlet that serves static content a standard (even if a de facto one) across servlet containers, or does its use restrict deployment to Tomcat / Jetty?

For example, 1 shows this method for getting the default dispatcher:

 final RequestDispatcher rd = getServletContext().getNamedDispatcher("default");

From a quick search it seems that this would also work on Jetty. How broadly will this technique work for obtaining a default servlet? For the servlet containers that have a default servlet, is it always a static content servlet?

Community
  • 1
  • 1
Jim Downing
  • 1,481
  • 12
  • 29
  • I don't understand the question. Servlets should work in all servlet containers that implement the correct standards. All servlet container will have some kind of fallback servlet, I think. Otherwise it would be pretty limited... – Patrick Cornelissen Nov 02 '09 at 12:48

4 Answers4

6

It's not a standard, but without it appservers can't serve static content. It's just crucial.

[edit] I saw you edited and elaborated your question in a more clear manner:

For example, [1] shows this method for getting the default dispatcher:

final RequestDispatcher rd = getServletContext().getNamedDispatcher("default");

From a quick search it seems that this would also work on Jetty. How broadly will this technique work for obtaining a default servlet? For the servlet containers that have a default servlet, is it always a static content servlet?

In that case, it may be a defacto standard, but I wouldn't rely much on that and for sure not code against implementation specific details or even defacto standards. Ask yourself: what's the sense/value of dispatching the request to the defaultservlet? Exactly, nothing.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • "Ask yourself: what's the sense/value of dispatching the request to the defaultservlet? Exactly, nothing." I just have to use it right now, Am I Evil ? – amirouche Mar 15 '11 at 20:25
  • 1
    @FMR: it boils down to that you don't need to dispatch to it at all. If you ever *need* to for some reason (for example because your servlet's `url-pattern` is too generic), then use a `Filter`. See also [this answer](http://stackoverflow.com/questions/870150/how-to-access-static-resources-when-using-default-servlet/3593513#3593513). Further, due to a bug on Tomcat versions before 6.0.29, mapping/dispatching to default servlet this way makes whole `WEB-INF` and `META-INF` folders public accessible. So yes, you're evil. – BalusC Mar 26 '11 at 02:31
  • @BalsucC, the link you provided solve exactly the problem I was facing. On GAE this is transparent, static files are automaticly served. – amirouche Apr 10 '11 at 13:11
  • @FMR: This does not happen automatically. This is a configuration setting which includes paths to static resources. Under the covers, it's the same principle as in the linked answer. Further, is this post still worth the downvote? – BalusC Apr 10 '11 at 13:18
  • I found dispatching to the default servlet useful. I have two copies of my static content, one with ES6 JavaScript and one with the JavaScript compiled to ES5, and I use differential serving based on the User-Agent to decide which to serve. – Robert Tupelo-Schneck Oct 13 '17 at 14:53
3

Servlet doesn't require a default servlet. However, the name must be "default" if one is defined. Can't imagine a container without default servlet. So you can assume it's standard.

See section SRV.11.1,

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.

ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
0

As long as the servlet container standard is the Servlet API you can see there is no such thing as a DefaultServlet. Most widely used servlet container have some defaults to run out of the box. But it is no "standard" requirement to implement a certain interface or abstract class so the container can run. ( A container can run even without any servlet).

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
0

Servlet Specification

The Servlet Specification does mention a "default" servlet, but there is no specification for it, it only mentions how the servlet container should use the default servlet in case it exists, but doesn't describe how the default servlet itself should be implemented.

The Servlet Specification specifies that if there is no URL mapping for a request:

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. Many containers provide an implicit default servlet for serving content.

The "/" URL mapping indicates the default servlet:

A string containing only the "/" character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.

Note that the word "indicates" is used, meaning that it is not a requirement.

The default servlet is usually used to serve static content, and is usually included in servlet containers, but it's not a requirement.

You can create your own default servlet (thereby overruling the one provided by the servlet container) by mapping a servlet to "/". Which usually means that it will disable static file serving, unless the selected servlet (or filters) does it. So if you have a simple servlet that only outputs a simple string like "Hello World", and set it as default servlet by mapping it to "/", then you disable things like 404 errors, because your simple servlet won't throw them. So then all (random) url requests that are not handled by more specific URL mappings, will be caught by the simple servlet.

Tomcat

Tomcat about their DefaultServlet:

The default servlet is the servlet which serves static resources as well as serves the directory listings (if directory listings are enabled).

Tomcat also says that you can change their default servlet:

If you need to change the DefaultServlet settings for an application you can override the default configuration by re-defining the DefaultServlet in /WEB-INF/web.xml. However, this will cause problems if you attempt to deploy the application on another container as the DefaultServlet class will not be recognised.

Jetty

Jetty also has a DefaultServlet.

This servlet, normally mapped to /, provides the handling for static content, OPTION and TRACE methods for the context.

Use cases

Change default behavior

You can use the DefaultServlet of your container in case you want to modify default behavior of the servlet container in case no url mapping is found. For example the behavior of directory listing, static file serving, etc.

RequestDispatching

Even though the answer that is currently marked as the solution says "Ask yourself: what's the sense/value of dispatching the request to the defaultservlet? Exactly, nothing.", there can be valid use cases.

In some cases, you may want to programmatically stop further filterchaining of your filters from within one of your filters, and forward the request to default behavior of the servlet container. In that case, you can forward to the DefaultServlet, which usually use the name "default", although this does make the app a bit container dependent. This can be done by using ServletContext.getNamedDispatcher("default")

So then you get something like this:

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
    if (someCondition) {
        // chain to next filter
        chain.doFilter(req, res);
    } else {
        // forward to DefaultServlet, for example, when you don't want 
        // further filters to be applied on the request of a static resource
        req
                .getServletContext()
                .getNamedDispatcher("default")
                .forward(req, res);
    }
}
Devabc
  • 4,782
  • 5
  • 27
  • 40