File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Default servlet and static content Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Default servlet and static content" Watch "Default servlet and static content" New topic
Author

Default servlet and static content

Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Howdy Ranchers!

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?


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1645
    
  25

Hi Pedro,
But what about welcome-files and static resources? How they can be accessed if I define the default servlet?

They can't.

For the static resources it makes sense: they all map to the default servlet.

For the welcome files it is a little bit more subtle. The specs actually say the following about welcome files:
The container will use for welcome-file's for appending to URIs when there is a request for a URI that corresponds to a directory entry in the WAR not mapped to a Web component.

Conclusion: the mapping to the default servlet has priority over the welcome files

Regards,
Frits
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Hi Frits, I understood your answer completely, except for this bit :

Frits Walraven wrote:For the static resources it makes sense: they all map to the default servlet.


How would all static resources map to a servlet?


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1645
    
  25

How would all static resources map to a servlet?

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
matches attempted:

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:
http://localhost:8080/AppName/fooStuff/foo.html

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).

Regards,
Frits
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Thanks for your explanaition Frits!

So the JSP files are reachable, as they match the rule no. 3 - they service is passed to the JSP Container (in Tomcat world it is Jasper), right?

Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1645
    
  25

So the JSP files are reachable, as they match the rule no. 3 - they service is passed to the JSP Container (in Tomcat world it is Jasper), right?

Yes, or rule number 1 or rule number 2 as you can define a jsp as a servlet, like:

Regards,
Frits
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

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).

Regards,
Frits


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?
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Thanks Frits, I did forget about this way of defining JSP's, so it's good to refresh the memory :-)

Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1645
    
  25

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?

You are right it depends on the design requirements. It can be a way of sending all the "wrong" request attempts to one handling Servlet.

The static resource in our example (foo.html) can of course easily be changed into a jsp-file (foo.jsp) and given a special (<servlet> and <servlet-mapping>) entry in the web.xml file, like:

This way you can still open a door to the foo.jsp (which doesn't really contain any non-static code)....

Regards,
Frits
Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183

Hey Frits . We are really thankful to you for your explanations.
I always look forward to it.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1645
    
  25

you are welcome
 
Consider Paul's rocket mass heater.
 
subject: Default servlet and static content
 
Similar Threads
jsf tutorial
servlet mapping clarification
Open Source Application Server
place of servlets
<welcome-file-list>