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] where as ServletConfig has a SESSION SCOPE.. [LOCAL SCOPE......which is mostly used for intialising purpose].
Hope my slight twisting of the definition helps.. :-)
ServletConfig is specific to a particular Servlet. It allows a Servlet to be given specific initialization parameters when the Servlet is loaded. Since the ServletConfig is specific to a Servlet, the parameters configured in ServletConfig can be used by any user accessing the Servlet. ServletConfig is Servlet specific, and not tied to any particular user or request-response cycle.
Just to confuse you, a ServletConfig is really associated with the ServletMapping in the web.xml file, and not the actual Java code that extends javax.blah.blah.blah.HttpServlet. So, you could have a single Java class called TaxServlet, but paramaterize it 52 times, so that a differently parameterized ServletConfig exists for 52 different Servlets configured in the web.xml file, each of which is used for calculating the taxes on one of each of the 52 states. (And yes, there are 52 American states. I'm including Canada and Mexico).
Okay, that last paragraph even confused me, but you get the point.
The ServletContext is like a global scope that represents an entire web based application, or as they are deployed, a war file. Parameters configured at the ServletContext level are available to all Servlets, JSPs, users, and request-response cylcles accessing a particular Servlet/JSP application.
I think that's a pretty high level, and simple overview of a relatively complex topic.
And just so you know, these ideas are also analogous to JSR-168 Portlet Programming with regards to the PortletContext and the PortletConfig object. Master the Servlet and JSP API, and everything else in the server-side world will fall into place.
-Cameron McKenzie [ November 08, 2006: Message edited by: Cameron W. McKenzie ]
posted 13 years ago
got the concepts, thanks folks for your explaination, thanks a lot
Thank & Regards, Tom
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop