Wednesday, April 21, 2010

How to Map URLs to servlets using web.xml / deployment descriptors?

The two deployment descriptors elements for URL mapping:
1) < servlet > - maps internal name to fully-qualified class name
2) < servlet - mapping > - maps internal name to public URL name

***** There can be more tags inside ****
< web - app ... >

***** The < servlet > element tells the Container which class files belong to a particular web application.****
  < servlet >
**** Ties a < servlet > element to < servlet -mapping > element. End user never sees this name ****
    < servlet -name >Internal identification1 < / servlet-name >
**** put the fully-qualified name of the class. without the ".class" extension. ****
    < servlet - class > foo.Servlet1 < / servlet - class >
  < / servlet >

  < servlet >
    < servlet - name >Internal identification2 < / servlet-name>
**** put the fully-qualified name of the class. without the ".class" extension. ****
    < servlet - class > foo.Servlet2 < / servlet -class >
  < / servlet >
..............................................................................................

**** Container uses at runtime when a request comes in, to ask, “which servlet should I invoke for this requested URL?” ****
  < servlet - mapping >
    < servlet - name > Internal identification1 < / servlet -name >
**** This is what client sees to ge to the servlet. Not the actual name. You can give anything. Can use a wild card as well inside this tag ****
    < url- pattern > /Public URL1 < / url - pattern >
  < / servlet - mapping >

  < servlet- mapping >
    < servlet - name >Internal identification2
    < url - pattern> / Public URL1 < / url - pattern >
 < / servlet - mapping >

No comments:

Post a Comment