aspose file tools*
The moose likes Servlets and the fly likes deployment Descriptor Servlet mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "deployment Descriptor Servlet mapping" Watch "deployment Descriptor Servlet mapping" New topic
Author

deployment Descriptor Servlet mapping

Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

In the following code from the book headfirst jsp and servlets

Basically we are using a servlet-name to refer to the java class and than mapping that name to the url pattern which the client will see
So My question is cant we directly map the class to the url .
some what like



Understanding is Everything - Peter Lord
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

No. You must use both the <servlet> and the <servlet-mapping> section.


OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5


Class needs to be the fully qualified package plus class name so the servlet container can find it. Classes used in servlets MUST be in packages to avoid all sorts of problems.

Way back at the beginning of the servlet API it was thought that it would be cool to allow for directly naming a servlet class in the URL. Thus we got the Invoker servlet.

These days everybody recognizes that this was a really really really bad idea since it prevented use of the full servlet API.

Bill
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

yup
Thank you peter Nelson and William Brogden (the link was good read)
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

Guys , but what are the benefits of having mapping in this way , did I miss anything ?

I still don't understand the motive behind such mapiing , having servlet mapped to name first and then name mapped to class or url-pattern .

I know this is the way it works but curious on "Why" part , why it was designed like this by Servlet designer.

http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

The objective of this kind of mapping is
1 . The actual servlet name and location available on the server would not be visible to the user , which would be necessary for security .
2 . Than there would be an internal name for that servlet know among the deployers which would be Chapter1 Servlet in the above case as specified.
3. The class name would be specific to developers to be known among them .
4. Maintainence would be definately easy as we would not have to keep on changing each and every JSPs if the servlet name or anything else changes , we can directly change the web.xml mapping which is basically global mapping of servlets for that particular application .
This is the basic understanding i got from Head first .
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
Also note that the "name" of a servlet class includes the package and may therefore be quite long.

Would your really like to type the whole com.mysite.stuff.thingee.BigServletWork name when mapping lets you call it "Work"? How prone to error is that?

Not even getting into the security hazard of having your whole package structure exposed everywhere.

Bill
T. Huy Nguyen
Ranch Hand

Joined: Nov 02, 2010
Posts: 57
William Brogden wrote:
Not even getting into the security hazard of having your whole package structure exposed everywhere.


I don't understand how that why separating servlet-class and url-pattern can prevent a security hazard. The 2 <servlet> and <servlet-mapping> are in the same document, any author of web.xml will be able to see the whole class name.

Would you mind explain further? Thanks


My material for SCJP (2008), SCWCD (2010), SCBCD (2010). About me
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
The full package and class name would be exposed to ALL USERS of your application.

The web.xml file can not be seen by outside users.

Bill
T. Huy Nguyen
Ranch Hand

Joined: Nov 02, 2010
Posts: 57
Perhaps you're referring to the security hazard of using Invoker servlet? If so, then I understand.

If not, then I still haven't understood why separating servlet-class and url-pattern can prevent a security hazard. Thanks though.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

One reason is because a single servlet can have multiple mappings.

Sure that could still be done via single XML element if you wanted, but it'd be inconsistent with the rest of the XSD.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Don't get me started about those stupid light bulbs.
 
subject: deployment Descriptor Servlet mapping