This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Context Vs Servlet Config" Watch "Servlet Context Vs Servlet Config" New topic
Author

Servlet Context Vs Servlet Config

Tom Lee
Greenhorn

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

Thanks in advance,
Tom
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

They're two completely different objects.
I recommend looking up the API for each and comparing the descriptions.
http://java.sun.com/j2ee/1.4/docs/api/index.html


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

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


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

Joined: Aug 26, 2006
Posts: 4968
    
    1

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.

Cheers!

-Cameron McKenzie
[ November 08, 2006: Message edited by: Cameron W. McKenzie ]
Tom Lee
Greenhorn

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

Thank & Regards,
Tom
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Context Vs Servlet Config