Friday, July 20, 2012

Difference between ServletContext and ServletConfig ?

What is the Difference between ServletConfig and ServletContext.?


ServletConfig:

public interface ServletConfig

ServletConfig encapsulates servlet configuration and gives access to the application (servlet context) object. Servlet initialization parameters appear in the servlet configuration file. Each servlet class may have several different servlet instances, one for each servlet parameters:


  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("myname"); 
  6. It is available only to the servlet in which init-param is configured.

Example: 


ServletContext:

public interface ServletContext

ServletContexts 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("myname"); 
  5. It is available to any servlet or jsp that is part of web application.


Example:

2 comments:

  1. This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you. Kopar at Newton Kampong Java Road

    ReplyDelete
  2. Thus, with the above features, Python training can very well act as a mentoring course to freshers. unindent does not match any outer indentation level python

    ReplyDelete