• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is the need for ServletConfig

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Arunabh Dash
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Nothing? Or something? Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic