File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet Context Vs Servlet Config Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Context Vs Servlet Config" Watch "Servlet Context Vs Servlet Config" New topic

Servlet Context Vs Servlet Config

Tom Lee

Joined: Aug 25, 2005
Posts: 15
I am new to servlet technology. Could anyone explain me, what is the diffrence between Servlet Context and Servlet Config.

Thanks in advance,
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

They're two completely different objects.
I recommend looking up the API for each and comparing the descriptions.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ajith George
Ranch Hand

Joined: Dec 22, 2005
Posts: 109
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...

where as
ServletConfig has a SESSION SCOPE.. [LOCAL SCOPE......which is mostly used for intialising purpose].

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

SCJP 1.4, Brainbench
LinkedIn - Blog
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

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 ]
Tom Lee

Joined: Aug 25, 2005
Posts: 15
got the concepts, thanks folks for your explaination, thanks a lot

Thank & Regards,
I agree. Here's the link:
subject: Servlet Context Vs Servlet Config
It's not a secret anymore!