• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

servlet and mapping declarations in web.xml

 
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..
 
Sheriff
Posts: 67269
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Sheriff
Posts: 67269
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Sheriff
Posts: 67269
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
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?
 
Bartender
Posts: 20842
125
Android Eclipse IDE Tomcat Server Redhat Java 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!