Tuesday, June 17, 2008

ServletConfig interface vs ServletContext interface in Java


ServletConfig interface

public interaface ServletConfig (javax.servlet) - A ServletConfig object is used by a Servlet Container to pass the initialization information to a servlet during its initialization. This object is built by the Servlet Container by using the information mentioned in the <init-param> element of the Deployment Descriptor (web.xml) for that particular servlet. If no initialization parameres are provided then a default ServletConfig object is built by the Container and passed to the servlet. A typical entry in the web.xml for this may look like:-

<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>Fully Qualified Class Name</servlet-class>
<init-param>
<param-name>NameOfTheInitParameter1</param-name>
<param-value>ValueOfTheInitParameter1</param-value>
</init-param>
<init-param>
<param-name>NameOfTheInitParameter2</param-name>
<param-value>ValueOfTheInitParameter2</param-value>
</init-param>
...
</servlet>

ServletContext interface

public interface ServletContext (javax.servlet) - this interface declares a set of methods used by a servlet to communicate with the Servlet Container. These methods are used by the servlets to get those tasks done which are controlled and maintained by the Servlet Container - example: writing to a log file, calling request dispatcher, fetching the MIME type of a file, etc.

Every JVM will have a separate ServletContext for a Web Application in case it is distributed (in this case the Web Application will be marked as "distributed" in its deployment descriptor file) across multiple JVMs. ServletContext can be understood as a gateway for the servlets to access the environment in which they execute.

<context-param> element can be used in the deployment descriptor file to specify the parameters, which will be used by the Container to build the ServletContext objects. These ServletContext objects will be unique per JVM per web application. A typical entry in web.xml for this may look like:-

<context-param>
<param-name>NameOfTheContextParameter1</param-name>
<param-value>ValueOfTheContextParameter1</param-value>
</context-param>
...

Every ServletConfig object contains a ServletContext object as well. Both the objects are created by the Servlet Container and the ServletConfig object is passed to every servlet while its initialization.

Difference between ServletConfig interface and ServletContext interface

Do I still need to mention the difference explicitly? ServletConfig may be unique for every servlet and it's used by the Servlet Container to initialize that particular servlet whereas ServletContext is same for all the servlets of one Web Application running on a JVM and it's used by the servlets as a gateway to access the environment in which they execute.



Share/Save/Bookmark


1 comment:

GeekDude said...

Nice points you have covered on ServletContext and ServletConfig. Here you can find some more points on ServletConfig and ServletContext.