Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the need for ServletConfig

 
Arunabh Dash
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Arunabh Dash
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply . I got what i was looking for .Again thank you very much

Regards
Arunabh Dash
 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic