Signup/Sign In

ServletContext Interface in Servlet

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.


Some Important method of ServletContext

MethodsDescription
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
void setAttribute(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

how to initialize servlet context using deployment descriptor


How to get the Object of ServletContext

ServletContext app = getServletContext();
OR
ServletContext app = getServletConfig().getServletContext();

Advantages of ServletContext

  • Provides communication between servlets
  • Available to all servlets and JSPs that are part of the web app
  • Used to get configuration information from web.xml

Difference between Context Init Parameters and Servlet Init Parameter

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

Example demonstrating usage 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 class :

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"));   
    }
}