• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Default servlet and static content

 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3412
320
Android Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hey Frits . We are really thankful to you for your explanations.
I always look forward to it.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are welcome
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic