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 <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]
where as
ServletContext has a SESSION SCOPE.. [LOCAL SCOPE......which is mostly used for intialising purpose].

Hope my slight twisting of the definition helps..    :-)

Gaurav

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:)

Vinay.

Which is which ;)
Both here are ServletContext
javax.servlet.ServletConfig has page scope
servletcontext  has application scope.

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.
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 ()

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" ?>
  <!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
             <display-name>ITT Web Application</display-name>
             <filter>
                         <filter-name>PhaseCheckFilter</filter-name>
                         <filter-class>com.solutionsny.itt.web.filter.PhaseCheckFilter</filter-class>
             </filter>
            <filter-mapping>
                         <filter-name>PhaseCheckFilter</filter-name>
                         <url-pattern>*.do</url-pattern>
             </filter-mapping>
            <servlet>
                         <servlet-name>action</servlet-name>
                         <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
                         <init-param>
                                      <param-name>application</param-name>
                                      <param-value>com.solutionsny.itt.web.ApplicationResources</param-value>
                         </init-param>
                         <init-param>
                                      <param-name>config</param-name>
                                      <param-value>/WEB-INF/struts-config.xml</param-value>
                         </init-param>
                         <init-param>
                                      <param-name>debug</param-name>
                                      <param-value>2</param-value>
                         </init-param>
                         <init-param>
                                      <param-name>detail</param-name>
                                      <param-value>2</param-value>
                         </init-param>
                         <load-on-startup>1</load-on-startup>
             </servlet>

             <servlet>
                         <servlet-name>ShowBinaryServlet</servlet-name>
                         <servlet-class>com.bea.content.manager.servlets.ShowBinaryServlet</servlet-class>
             </servlet>

             <servlet-mapping>
                         <servlet-name>action</servlet-name>
                         <url-pattern>*.do</url-pattern>
             </servlet-mapping>

             <servlet-mapping>
                         <servlet-name>ShowBinaryServlet</servlet-name>
                         <url-pattern>/getContent/*</url-pattern>
             </servlet-mapping>

             <welcome-file-list>
                         <welcome-file>/logon.do</welcome-file>
             </welcome-file-list>

             <error-page>
                         <error-code>404</error-code>
                         <location>/notfound.do</location>
             </error-page>

             <error-page>
                         <error-code>400</error-code>
                         <location>/badrequest.do</location>
             </error-page>

             <error-page>
                         <error-code>403</error-code>
                         <location>/forbidden.do</location>
             </error-page>

             <error-page>
                         <error-code>500</error-code>
                         <location>/servererror.do</location>
             </error-page>

             <taglib>
                         <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
                         <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
             </taglib>

             <taglib>
                         <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
                         <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
             </taglib>

             <taglib>
                         <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
                         <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
             </taglib>

             <taglib>
                         <taglib-uri>/WEB-INF/taglibs-string.tld</taglib-uri>
                         <taglib-location>/WEB-INF/taglibs-string.tld</taglib-location>
             </taglib>
</web-app>

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>
    <param-name>Developer</param-name>
    <param-value>Crazy@weird.com</param-value>
</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 =
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:

<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.

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..
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??

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..
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

Vinay.

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : 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.