Head First Servlets and JSP has a very clear and easy-to-understand explanation of these two objects. In a nutshell, servlet config pertains to a single servlet and servlet context pertains to an entire application. Both are configurable in the deployment descriptor (web.xml). I am beginning to believe there is no excuse for not owning a copy of Head First Servlets and JSP.
sample code to use servletconfig and servletcontext.
to start with, You need to know about web.xml, here is a sample web.xml
<servlet> <servlet-name>Testing</servlet-name> <description>This is just for testing</description> <servlet-class>mypackage.com.Testing</servlet-class> <init-param> <param-name>useMe</param-name> <param-value>Hurray..I am being used</param-value> </init-param> </servlet>
ServletContext context = getServletContext(); ServletConfig config = context.getServletConfig (); Enumeration enum = config.getInitParameters (); //or else you can use String value = config.getInitParameter("useMe"); //print "value" and you will see "Hurray..I am being used" NOTE that getinitparam is only available for this servlet since this is declared in web.xml. other servlets have to define their own init-params in web.xml. This implies that every servlet will have "its own" servletconfig.
Now Servlet Context,
ServletContext context = getServletContext(); String value = context.getInitParameter("locale"); //print "value" and you will see "utf-8"
Any servlet within the web application can use the context param. hence ServletConfig is available to all the servlets inside the webapp.
Originally posted by H Wilson: I am beginning to believe there is no excuse for not owning a copy of Head First Servlets and JSP.
Why?? Is it become free? I saw a mate looking for a free legal copy of it, like we have "Core Servlet & JSP", and "Servlets and JavaServerPages" legally free.
Joined: Feb 23, 2005
Originally posted by Adeel Ansari: [QB]
Why?? Is it become free? QB]
Certainly lack of funds is a legitimate excuse for not owning Head First books. I just find that so many of the basic questions about Servlets and JSP are answered very, very clearly and simply in that book. For that reason I would recommend a new Java programmer to read that book first and the Sun documentation second. It's definitely a great place to start if Servlets and JSP are new to you. Some people may be annoyed by the "dumbed-down" approach, but I find I can get past that because the information is so clearly explained.
Joined: Oct 11, 2004
Thanks a lot Sripathi , I got it very well . but why we need this :
if such a URL comes to your application, http://yourserver.yourdomain:yourport/yourapplication/XXX (yourapplication-->context) then the servlet container look at the servlet mappings in your web.xml. and find the servlet that corresponds to /XXX and will send the client's request to that particular servlet.
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.
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.
Are they just used for sending initialization parameters or is there any other use as well?
Joined: Mar 07, 2005
Any servlet within the web application can use the context param. hence ServletContext is available to all the servlets inside the webapp.
ServletConfig is for that servlet or the page. ServletContext is for the entire application. //this means that the context can be used to set and get paramaters from all the servlets. init-param is one way to do this. You can also use the below methods in ServletContext