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.
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 <context-param> 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. Murugesa pandian Just to add more clearity... ServletContext has a APPLICATION SCOPE .. [GLOBALLY ACCESSIBLE
ACROSS THE PAGES]
Hope my slight twisting of the definition helps.. :-) Gaurav Looks like you were in a hurry when you typed the reply....
Vinay. Which is which ;)
Any other suggstions? Jon Guys.. I think I did a mistake .. seems I need to take a proper rest before giving out more wrong info.. Vinay thanks for the update and correcting me [I did a blunder].. Gaurav Whatever you know about them, is correct and enough.
To the max what I know is the same you wrote in your mail. You can write ::: ServletConfig config = context.getServletConfig () Config.getInitparameters (); Like in the example below (web.xml) In org.apache.struts.action.ActionServlet, you can read it init parameter viz “application”, “config”, “debug” & “detail”. In order to read it in init () method of servlet, you need servletconfig, for which you need context first J Hope this helps, but if it is stilln’t clear try to code an example. <?xml version="1.0" encoding="UTF-8" ?>
<servlet>
<servlet-mapping>
<servlet-mapping>
<welcome-file-list>
<error-page>
<error-page>
<error-page>
<error-page>
<taglib>
<taglib>
<taglib>
<taglib>
Sanjeev Dhiman 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. <context-param>
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 =
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: <servlet>
The above entry into a web.xml file is for a servlet named
Select. <Please note that you are writing the entry within a Servlet
tag which means that this is for a particular servlet. Unlike for ContextParamter
where we did not write under any particular 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. Vinay. Thx alot for all the wonderful info tht you ppl have provided
to clear my query.. these have been of immense help to me..
Can some1 pls explain
Kiruthiga Vinnny too much info.. is bad as well..better make it in a KISS format..Keep It Simple & Short. No matter your info was good... Kiruthiga...to put it simply.. Serlvet Container as the name suggests manages the Servlets and its the Life cycle... Now coming back to the Managing part.. I mean basically takes care of Environmental aspects of a Servlets [creation / execution / deletion to put in simple words]. Hope this simple info.. gives a broader sense, where lots of books was unable to explain to u.. Gaurav >Vinnny too much info.. is bad as well..
> better make it in a KISS format..Keep It Simple &
Short.
> no matter ur info was good...
Vinay.
Do you have a Java Problem?
Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|