Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why servlet - mapping?  RSS feed

 
pradeep arum
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Servlets...and my question is why is servlet-mapping required in web.xml ...whe i use <form action=servletclassname > from the .html file, what is the necessity of < servlet-mapping > tag when i can direct all the requests from the <form action> tag of the .html file...please throw some light
thanks
Pradeep
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refering to a servlet by its class name is not the preferred way to access a servlet. By default, many servlet containers don't support this behaviour.

Some of the problems with using this servlet is that
  • all servlets are available if you use this. It means you have to delete a servlet to 'disable' it. People may be able to access servlets you don't intend just by knowing its name.
  • it only allows a one to one mapping between URLs and servlet classes. If you had a servlet that did the same thing but behaved slightly differently depending on the mapping location, this wouldn't be possible using the classname.
  • there are other parts of the Servlet API that use the mapped name of the srevlet. I have no idea whether they work correctly, but you wouldn't know until you tried, and it would be unfortunate if you found a feature didn't work until it was too late to back out.

  • These are just some differences off the top of my head, but there may also be an official explaination. I belive that some containers don't support referring to servlets by class name at all.

    Dave
     
    Jeroen Wenting
    Ranch Hand
    Posts: 5093
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    to illustrate the second point, we have a single servlet that depending on the name it's called with looks up a context param from its mapping and loads a configuration which tells it what to do.
    Before I built that servlet the practice was to generate a new servlet where the only difference between the (at that moment already dozens, would be literally hundreds now) classes was the name of the configfile to be used.
    The mapping would still be required in order to give decent looking URLs...
     
    William Brogden
    Author and all-around good cowpoke
    Rancher
    Posts: 13078
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sun's intent with the servlet and JSP APIs is to allow you to create a "web application" that is completely independent of all other web applications on a server, yet is flexible in terms of configuring without changing the source code. Thats why web.xml provides so many features.
    Bill
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!