File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes What is the need for ServletConfig Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "What is the need for ServletConfig" Watch "What is the need for ServletConfig" New topic

What is the need for ServletConfig

Arunabh Dash

Joined: Apr 05, 2006
Posts: 20
What is the need of a special ServletConfig object for every servlet ?

Let me be more precise . I can get the initial parameters from a ServletConfig
object . But that i can do the same inside a servlet also . I can make a data member in my extended Servlet class like :-
public class MyServlet extends HttpServlet
String name;

And i can initialize that member in the overridden init() method of the MyServlet class .
Again from my SertvletConfig i can get a reference to ServletContext . But straight way by using getServletContext() method inside the servlet a can get the same .

Then why i need ServletConfig object .

With regards
Arunabh Dash
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

java.lang.String)" target="_blank" rel="nofollow">

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Arunabh Dash

Joined: Apr 05, 2006
Posts: 20
Actually i am questioning on the need of ServletConfig . What is the special thing i can do with ServletConfig ,that i can't do with any other object ???

With Regards
Arunabh Dash
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
As Ben pointed out, you use it to access the servlet init parameters. Do not be fooled by the init parameters you access through ServletContext - those are the parameters of the web app, not any particular servlet.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
What would you do in case you want to give certain information to one servlet during initializatiob but not to all the servlets.In the case you can set init param for that particular servlet.Now only that servlet can get those values that you have set using the webb.xml , not all the servlerts.

Rahul Bhattacharjee
LinkedIn - Blog
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Well, the ServletConfig configures a Servlet.

I always give the example of a Tax Servlet. You can code one, and define it 52 times, once for every state in the USA. Each definition in the web.xml file gets a different parameter for the STATE_TAX init_parameter. You have a FloridaTaxServlet and a TexasTaxServlet and a NevadaTaxServlet etc.

Behind the scenes, there is only one chunk of Java code, but the ServletConfig allows multiple 'clones' to be made with their own parameterization.

That's a big benefit of ServletConfig - the ability to create templatized Servlets.


-Cameron McKenzie
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63873

Originally posted by Cameron W. McKenzie:
Well, the ServletConfig configures a Servlet.

Ummm, no, that's misleading.

The deployment descriptor configures the servlet. The ServletConfig merely reports the details of the configuration.

[Asking smart questions] [About Bear] [Books by Bear]
Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
ServletConfig is a configuration object used by a servlet container to pass information to a servlet during initialization. Before calling the service method to a request it calls the init(ServletConfig) to initilize the servlet. At this time it loads all the params the servlet needed. If you need some parameters specific to a Servlet you can make use of <ServletConfig> in deployment descriptor file. The init parameters in <ServletContext> are for the entire application(i.e, every servlet in the application can make use of the init-parameters in the ServletContext).

Look at the below example..

<!-- The TCP port of application server.-->

In the above deployment descriptor file the param-name "APPLICATION_SERVER_PORT" is needed for all the servlets that is why it is kept in context-param( instead of writing that for each and every servlet). Where as for the param-name "validate" is for the servlet only in which it is mentioned. It loads all of the parameters that are mentioned in <servlet-config> before it calls service method.

Try to reduce the usage of number of context-params. Because these are not thread safe.
Arunabh Dash

Joined: Apr 05, 2006
Posts: 20
Thanks for the reply . I got what i was looking for .Again thank you very much

Arunabh Dash
Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
I agree. Here's the link:
subject: What is the need for ServletConfig
It's not a secret anymore!