ServletContext Interface


For every Web application a ServletContext object is created by the web container. ServletContext object is used to get configuration information from Deployment Descriptor(web.xml) which will be available to any servlet or JSPs that are part of the web app.

Advantage of ServletContext


Easy to maintain if any information is shared to all the servlet, it is better to make it available for all the servlet. We provide this information from the web.xml file, so if the information is changed, we don't need to modify the servlet.

Usage of ServletContext Interface


There can be a lot of usage of ServletContext object. Some of them are as follows:

  1. The object of ServletContext provides an interface between the container and servlet.
  2. The ServletContext object can be used to get configuration information from the web.xml file.
  3. The ServletContext object can be used to set, get or remove attribute from the web.xml file.
  4. The ServletContext object can be used to provide inter-application communication.

servletcontext

Some Important method of ServletContext


Methods Description
Object getAttribute(String name) returns the container attribute with the given name, or NULL if there is no attribute by that name.
String getInitParameter(String name) returns parameter value for the specified parameter name, or NULL if the parameter does not exist
Enumeration getInitParameterNames() returns the names of the context's initialization parameters as an Enumeration of String objects
voidsetAttribute(String name,Object obj) set an object with the given attribute name in the application scope
void removeAttribute(String name) removes the attribute with the specified name from the application context

How Context Parameter is Initialized inside web.xml

servlet context

How to get the Object of ServletContext


ServletContext app = getServletContext();

or

ServletContext app = getServletConfig().getServletContext();

Difference between Context Init Parameters and Servlet Init Parameter

Context Init parameters Servlet Init parameter
Available to all servlets and JSPs that are part of web Available to only servlet for which the <init-param> was configured
Context Init parameters are initialized within the <web-app>not within a specific <servlet> elements Initialized within the <servlet> for each specific servlet.
ServletContext object is used to get Context Init parameters ServletConfig object is used to get Servlet Init parameters
Only one ServletContext object for entire web app Each servlet has its own ServletConfig object

Example of ServletContext

web.xml
	

 <web-app ...>



   <context-param>

       <param-name>driverName</param-name>

       <param-value>sun.jdbc.JdbcOdbcDriver</param-value>

   </context-param>

   

   <servlet>

      <servlet-name>hello</servlet-name>

      <servlet-class>MyServlet</servlet-class>

   </servlet>

   

   <servlet-mapping>

        <servlet-name>hello</servlet-name>

        <url-pattern>/hello</url-pattern>

   </servlet-mapping> 

     

</web-app>

				

				

MyServlet.java
	

 import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;



public class MyServlet extends HttpServlet {



  protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        ServletContext sc = getServletContext();

        out.println(sc.getInitParameter("driverName"));   

    }

}