This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Servlets and the fly likes Configuring Servlets  in web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Configuring Servlets  in web.xml" Watch "Configuring Servlets  in web.xml" New topic

Configuring Servlets in web.xml

RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
If we have 100's of servlets in a web application , do we need to declare all of them in web.xml or is there any option for this ??

If you want something you never had do something which you had never done
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32319

You need to declare all your servlets. Some servers let you use the classname instead, but this is less user friendly and less secure.

Why do you have so many servlets? Most people have just one (or a few) controller servlets and control the rest via URLs.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Edwin Stephens

Joined: Dec 30, 2008
Posts: 15
Hello RaviNada,

If you are using Tomcat, you can accomplish this via the Invoker Servlet (org.apache.catalina.servlets.InvokerServlet) in Tomcat's web.xml file. It allows you to execute servlet classes without needing to map them in web.xml.

However, in general this is not a good practice, since servlet mappings function to prevent users from accessing resources that they shouldn't have access to. Also, hundreds of servlets seems excessive! You may want to consider rolling functionality into fewer servlets, and when you do this, mapping becomes much more manageable.


RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Thanks Edwin ,

what if the server is Weblogic ??Is there any option avialable like this ??
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63057

If you want to limit the number of servlets to configure, use a Front Controller. If you want an easy-to-use implementation of one, check out my own Front Man package (follow link in my signature).

Avoid the invoker! Bad bad idea.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Thanks , nice solution
Rajat Raina

Joined: Feb 17, 2009
Posts: 10
Front controller is a good option
To be specific you can use struts framework

To be a winner first you need to be a begineer.
Raina Rajat
SCWCD (95%)
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Rajat Raina wrote:Front controller is a good option
To be specific you can use struts framework

I think it´s a bit too late to introduce a MVC framework as he already has hundred servlets. You can better start off with a MVC framework from the zero on than applying it on an existing application, otherwise you´re going to waste a lot of time on changing.

A front controller is a better solution. I only hope that his servlets are well designed (thus not tight coupled with business and data logic) otherwise there is still a lot of rewrite needed.
I agree. Here's the link:
subject: Configuring Servlets in web.xml
It's not a secret anymore!