Monday, October 1, 2012

Difference between Servlet Config and Servlet Context

 Difference between Servlet Config and Servlet Context


Yes, they are extremely basic.

ServletConfig - parameter scope is limited to the scope of the servlet in which the parameter is defined. For example, shopping cart of a user is specific to a particular user so servletconfig params can be used.

ServletContext - parameter scope is application wide and the parameter can be use to store application session info/ application initialization parameters,etc not specific to a servlet.

Actually the servlet container will create ServletConfig object and pass it to the servlet when it's initialized. Servlet container does object creation for other interface types HTTP request, response etc.

ServletConfig has servlet wide scope. It is defined inside in web.xml

ServletContext has application wide scope . It is defined outside the tag in web.xml

---------------
ServletConfig
---------------
One ServletConfig per servlet
it is used to pass deploy-time info to servlet and configured in the deployment descriptor file, such as web.xml.
it is used to access ServletContext
It is within the Servlet element in Deployment descriptor.
It is accessed by using getServletConfig().getInitParameter("myname");
It is available only to the servlet in which init-param is configured.
 


--------------
ServletContext
---------------
It returns the current context of a web application running in a particular JVM.
if the web application is distributed,it is one per JVM.
It is used to access the elements configured in deployment descriptor.
It is accessed by using getServletContext().getInitParameter("myname");
It is available to any servlet or jsp that is part of web application.


  

No comments:

Post a Comment