Tuesday, September 14, 2010

Difference between ServletContext and ServletConfig

What the difference between ServletContext and ServletConfig apart from the fact that Context gives environmental details and Config abt the initialisation params?? Another other substantial diff??

I have mentioned some tips regarding ServletContext and ServletConfig. okey Do well

ServletContext Defines a set of methods that a servlet uses to communicate with its servlet container.
ServletConfig is a servlet configuration object used by a servlet container used to pass information to a servlet during initialization. All of its initialization parameters can ONLY be set in deployment descriptor.

The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.

You can specify param-value pairs for ServletContext object in tags in web.xml file.

The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets.

The ServletContext parameters are specified for an entire application outside of any particular servlet and are available to all the servlets within that application.



For ex: If you want the name of your company or your E-mail address appear in all the pages of your web application then you use ServletContext.



Developer
Crazy@weird.com











In the above xml entry you set the value of Context Parameter i.e Developer as Crazy@Weird.com. By making the above entry into your web.xml file you can get the value of the ontext parameter 'Developer' anywhere throughout your web application. You can have any number of such context parameters.

String s =
getServletContext.getInitParameter("Developer");

You can have the above statement in any of your servlets to get the value of the String s as "Crazy@Wierd.com". So you are setting the parameter Developer in the web.xml as a Context paramter (which is available throughout the webapp). You get these Context Paramters using a ServletContext object as shown in the statement above.

Coming to ServletConfig, its a more specific one. The ServletContext is for the whole web-app but the ServletConfig is for one particular Servlet.

When you want paramters which cannot be hardcoded in your servlet you use ServletConfig. I cant think of a good example at the moment. Lets do with a lame one;). Lets consider you need to have a String value which tells you what a particular servlet does. Remember ServletConfig is for a particular servlet and not for the entire Application.

The web.xml file may look like this:



Select
Select

About the Servlet
This Servlet gives you a list of
colors to select from








The above entry into a web.xml file is for a servlet named Select. Within the Servlet tag you set the value for a Servlet paramter called "About the Servlet".

So whenever you do getServletConfig().getInitParamter("About the Servlet") you will get a string "This Servlet gives you a list of colors to select from". You get this value only when you call within the servlet called Select because you have set the paramter only for that particular servlet. Unlike context paramters which you could access anywhere using ServletContext.

To sum it up, every servlet has the same ServletContext but it also has its own ServletConfig.



http://www.sap-img.com/java/difference-between-servletcontext-and-servletconfig.htm

No comments:

Post a Comment