• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Why JSP needs to be mapped with slash (/), while Servlet does not?

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When doing mappings in my web.xml file, I came across using slashes which use I clearly don't understand properly. That is, why when mapping JSP, I must prepend (/), while for Servlet I do not?

Example (I have result.jsp and MyServlet.java):


Thanks
 
Saloon Keeper
Posts: 24557
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's an incorrect specification.

First of all, you don't need to mark JSPs in web.xml because the ".jsp" at the end of the URL is a dead giveaway to the server that it's a JSP and what its URL resource path should be.

Secondly, servlet-class is supposed to be the fully-qualified class name (including package) of the servlet. In other words, not "MyServlet", but "com.coderanch.mywebapp.MyServlet".

In JEE, you locate a servlet via a 2-part mechanism. The first part maps a URL pattern(s) to a logical servlet ID. The URL patter generally does contain a leading slash because that indicates the root of the webapp resource tree. But there are cases where you're mapping wildcard URL patterns and one of the classic ones is "*.jsf", which is used to detect and dispatch URLs to the FacesServlet of Javaserver Faces. In which case you don't want a leading slash (absolute path), or you won't be able to easily partition JSF view definitions into sub-directories.

Once you have a logical servlet ID, web.xml is then scanned for a binding from logical ID to Servlet-Class and an instance of that class is located or instantiated and given the incoming URL request.

All of the above also apply in the case of JEE annotations, since they simply cause web.xml to be built by the annotation processor.

 
Saloon Keeper
Posts: 7174
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:That's an incorrect specification.


The JSP part looks correct. I can't be bothered to find it in the servlet spec right now, but https://docs.oracle.com/cd/E11035_01/wls100/webapp/configurejsp.html#wp162442 (about WebSphere, not JSP in general, but at least coming from Oracle) indicates that this is exactly how one should map a JSP file. I'll grant you that it is an odd (and undesirable) thing to want to do, but perfectly legal nonetheless.
 
Tim Holloway
Saloon Keeper
Posts: 24557
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct. That would be the mechanism you'd use if you wanted to "hide" the actual resource location (and filename extension) of a JSP and make it look exactly like a servlet from the viewpoint of a URL.

If is very definitely not common practice, however, since virtually all modern webapp frameworks dispatch into a servlet. And/or simply use the ".jsp" extension in their URLs.
 
Stefan Jankovic
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I understand everything what you are saying. And my code works perfectly fine

I am sorry, but I still failed to understand why there is slash before JSP and not before Servlet mapping in XML file. Also, another confusing part is that when I do annotation mapping there is slash (unlike XML file that shouldn't have it). Damn it, this is so confusing

Thanks again!
 
Tim Holloway
Saloon Keeper
Posts: 24557
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have 3 types of data here.

1. Servlet-name. This is an identifier used to link a URL(s) to a servlet. It NEVER contains a slash, because it's just an ID. It's a simple word chosen by you and not visible to anything else but the server's container mapper.

2. URL-pattern. This is a wild-card expression representing a URL resource path. That is, everything after the "http://hostname:1234/mywebapp" part of a URL and before the funny characters like "?" or ";".

A URL-pattern can be absolute or relative. For example, /myservlet is absolute. "*foo*" is relative - any URL with "foo" anywhere in its resource path matches, including "/mywebapp/foo/bar" and "/genericappfoo123".

3. A servlet classname. As I said, this must be a fully-qualified class name such as com.javaranch.foo.servlets.MyWebapp. This NEVER has a slash in it because they're not valid Java classname/package component characters.

Note: Edited #3 above because I mistyped a slash into it.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic