aspose file tools*
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "What is the need for ServletConfig" Watch "What is the need for ServletConfig" New topic
Author

What is the need for ServletConfig

Arunabh Dash
Greenhorn

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
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Hint:
java.lang.String)" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletConfig.html#getInitParameter(java.lang.String)


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

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
Marshal

Joined: Mar 22, 2005
Posts: 41886
    
  63
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.


Ping & DNS - my free Android networking tools app
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
    
    1

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.

Cheers!

-Cameron McKenzie
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61232
    
  66

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] [Bear's FrontMan] [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..

<web-app>
<context-param>
<!-- The TCP port of application server.-->
<param-name>APPLICATION_SERVER_PORT</param-name>
<param-value>7777</param-value>
</context-param>
<servlet>
<servlet-name>SERVLET</servlet-name>
<servlet-class>WEB.MYSERVLETS.SERVLETCLASS</servlet-class>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
</web-app>

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.

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

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

Regards
Arunabh Dash
Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the need for ServletConfig