Powered by Blogger.

Difference between ServletContext and ServletConfig

>> Thursday, May 19, 2011

What is the difference between ServletConfig and ServletContext?
ServletContext and ServletConfig both are interfaces.ServletContext is one per application and within the application serveral Servlets will be defined. Each Servlet will be having a ServletConfig object. Even for jsp page.You can understand easily by the image given below.

Difference between ServletConfig and ServletContext_JavabynataraJ


Signature: public interface ServletConfig

ServletConfig encapsulates Servlet configuration and gives access to the application (ServletContext) object. Servlet initialization parameters appear in the Servlet configuration file. Each servlet class may have several different servlet instances, one for each servlet parameters.
These are the main points to remember for ServletConfig:
  1. One ServletConfig per servlet
  2. It is used to pass deploy-time info to servlet and configured in the deployment descriptor file. 
  3. it is used to access ServletContext 
  4. It is within the Servlet element in Deployment descriptor.
  5. It is accessed by using getServletConfig().getInitParameter("testName"); 
  6. It is available only to the servlet in which init-param is configured.


Signature: public interface ServletContext

ServletContext encapsulate applications. Applications are generalized virtual hosts. a URL prefix defines a distinct application. So /myapp and /yourapp could define different applications. As a degenerate case, each virtual host has its own ServletContext.

  1. It returns the current context of a web application running in a particular JVM.. 
  2. If the web application is distributed,it is one per JVM. 
  3. It is used to access the elements configured in deployment descriptor.
  4. It is accessed by using getServletContext().getInitParameter("testName"); 
  5. It is available to any servlet or jsp that is part of web application.


Reference Books:

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2019. All rights reserved.