5

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,471
  • 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

3 Answers3

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,040,783
  • 362
  • 3,548
  • 3,513
  • "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
  • 72,880
  • 29
  • 134
  • 165
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
  • 23,364
  • 13
  • 69
  • 108