• Post Reply Bookmark Topic Watch Topic
  • New Topic

servlet and mapping declarations in web.xml

 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,
While programming the Servlets and using the web.xml, we write the below format:

<servlet>
<servlet-name>xmlServlet</servlet-name>
<servlet-class>main.programs.XMLServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>xmlServlet</servlet-name>
<url-pattern>/XMLServ</url-pattern>
</servlet-mapping>

My question is why can't the below structure be configured to avoid redundancy?
By putting all the required data under one single tag instead of 2 tags ,i.e the <servlet> and <servlet-mapping> tag.


<servlet>
<servlet-name>xmlServlet</servlet-name>
<servlet-class>main.programs.XMLServlet</servlet-class>
<url-pattern>/XMLServ</url-pattern>
</servlet>

By making this kind of lots of redundant code can be reduced.

Please let me know if there is a reason if it cannot be done and correct me if i am wrong in any sense.

Cheers..
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the mappings are orthogonal to the servlet declarations, and there can be many mappings for each servlet declaration.
 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,
Thanks for the quick reply.
Can you please elaborate?

Yes,even if there are many mapping,in my example i am just trying to remove the occurance of the <servlet-name> ,along with the <servlet-mapping> tag as a whole.
With only 1 <servlet-name> can't it be made possible to pick the servlet having a particular URL Pattern?

Thanks in advance..
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What it simply comes down to is that whether you agree with it or not, the XML Schema doesn't allow for such shortcuts, so you can't do it.
 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,
Thanks for the info..

I do agree with you that the existing XML Schema does not entertain the kind of structure i have shown.
However can it be changed ?
Are there more intricacies involved for not doing this?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keshav pradeep ramanath wrote:However can it be changed ?

I guess you can petition the framers of the next version of the Servlet Specification to make such a change. But they're likely to simply ignore such a request. I know I would.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you use annotations instead of XML?
 
Tim Holloway
Bartender
Posts: 18417
60
Android Eclipse IDE Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole mechanism is designed to maximize abstraction and flexibility. Although at first glance, it certainly can seem confusing.

Often you only have a simple URL pattern(s) that you want to feed to a servlet, but sometimes you need more. Furthermore, by giving a servlet a logical name instead of pointing the URLs directly to the physical servlet, you have the flexibility to shift URLs to an alternative servlet (for example, a debugging version of the original servlet) and minimise the number of web.xml entries you need to modify to do that on a selective basis.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!