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 <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.
Just to add more clearity...
ServletContext has a APPLICATION SCOPE .. [GLOBALLY ACCESSIBLE ACROSS THE PAGES]
ServletContext has a SESSION SCOPE.. [LOCAL SCOPE......which is mostly used for intialising purpose].
Hope my slight twisting of the definition helps.. :-)
Looks like you were in a hurry when you typed the reply....
"ServletContext has a SESSION SCOPE" may be you wanted to say ServletConfig instead of ServletContext
there...but even then it would have been wrong since ServletConfig does'nt have sessionscope to be precise:)
Which is which ;)
Both here are ServletContext
javax.servlet.ServletConfig has page scope
servletcontext has application scope.
Any other suggstions?
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]..
Whatever you know about them, is correct and enough.
What else you want to know about them.
To the max what I know is the same you wrote in your mail.
You can write :::
ServletConfig config = context.getServletConfig ()
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" ?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
<display-name>ITT Web Application</display-name>
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.
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:
<param-name>About the Servlet</param-name>
<param-value>This Servlet gives you a list of
colors to select from</param-value>
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
tag since it was for the whole application.> 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.
Thx alot for all the wonderful info tht you ppl have provided to clear
my query.. these have been of immense help to me..
I need yet another clarification frm you ppl again!!! :-)
Can some1 pls explain
What is a Servlet Container?
Ii have browsed thro several articles but cldnt find a simple and satisfactory explanation. If some1 cld pls explain this to me??
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..
>Vinnny too much info.. is bad as well..
Thank you for the feedback
> better make it in a KISS format..Keep It Simple & Short.
Infact I tried to make it in the so called KISS format that is why the reply is short ;)
> no matter ur info was good...
thanks a lot man
Do you have a Java Question?
Java Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity. Information used on this site is at your own risk.
The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.