Difference between ServletContext and ServletConfig
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.
ServletConfig:
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:
ServletContext:
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.
Example:
Reference Books:
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.
ServletConfig:
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:
- One ServletConfig per servlet
- It is used to pass deploy-time info to servlet and configured in the deployment descriptor file.
- it is used to access ServletContext
- It is within the Servlet element in Deployment descriptor.
- It is accessed by using getServletConfig().getInitParameter("testName");
- It is available only to the servlet in which init-param is configured.
<servlet> <display-name>InitParamTest</display-name> <servlet-name>ServletConfigTest</servlet-name> <servlet-class>test.ServletConfigTest</servlet-class> <init-param> <param-name>testName</param-name> <param-value>ConfigValue</param-value> </init-param> </servlet>
ServletContext:
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.
- 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("testName");
It is available to any servlet or jsp that is part of web application.
Example:
<context-param> <param-name>globalVariable</param-name> <param-value>javabynataraj.blogspot.com</param-value> </context-param>
Reference Books:
- Core Servlets and JavaServer Pages : Advanced Technology
- Core Servlets and JavaServer Pages: Core Technologies
- Java Servlets by Example 01 Edition
- Head First Servlets And JSP 2nd Edition