Just to add more clearity...
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>
<param-name>Developer</param-name>
<param-value>
[email protected]</param-value>
</context-param>
In the above xml entry you set the value of Context Parameter i.e Developer as
[email protected]. 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 "
[email protected]". 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>
<servlet-name>Select</servlet-name>
<servlet-class>Select</servlet-class>
<init-param>
<param-name>About the Servlet</param-name>
<param-value>This Servlet gives you a list of
colors to select from</param-value>
</init-param>
</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
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.