Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Context Vs Servlet Config

 
Tom Lee
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ajith George
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.. :-)
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got the concepts, thanks folks for your explaination, thanks a lot

Thank & Regards,
Tom
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic