In the last tutorial, we discussed ServletConfig, the Servlet Container creates ServletConfig object for each Servlet during initialization. The main difference between ServletConfig and ServletContext is that unlike ServletConfig, the ServletContext is being created once per web application, i.e. ServletContext object is common to all the servlets in web application.
This is how we can create ServletContext object. In this code we are creating object in init() method, however you can create the object anywhere you like.
ServletContext sc; public void init(ServletConfig scfg) { sc=scfg.getServletContext(); }
Once we have the ServletContext
object, we can set the attributes of the ServletContext
object by using the setAttribute()
method. Since the ServletContext object is available to all the servlets of the Web application, other servlets can retrieve the attribute from the ServletContext object by using the getAttribute()
method.
Context Initialization Parameter
Context Initialization parameters are the parameter name and value pairs that you can specify in the deployment descriptor file (the web.xml file). Here you can specify the parameters that will be accessible to all the servlets in the web application.
When we deploy the Web application, the Servlet container reads the initialization parameter from the web.xml file and initializes the ServletContext object with it. We can use the getInitParameter()
and getInitParameterNames()
methods of the ServletContext interface to get the parameter value and enumeration of parameter names respectively.
For example, here I have specified the parameter email_id with the value, since this is common to all the servlets, you can get the parameter name and value in any servlet.
<context-param> <param-name>email_id</param-name> <param-value>[email protected]</param-value> </context-param>
ServletContext complete example: To get the initialization parameters
In this example we have two context initialization parameters (user name and user email) in web.xml file and we are getting the value in Servlet using getInitParameter() method that returns the value of given parameter.
DemoServlet.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DemoServlet extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html"); PrintWriter pwriter=response.getWriter(); //ServletContext object creation ServletContext scontext=getServletContext(); //fetching values of initialization parameters and printing it String userName=scontext.getInitParameter("uname"); pwriter.println("User name is="+userName); String userEmail=scontext.getInitParameter("email"); pwriter.println("Email Id is="+userEmail); pwriter.close(); } }
web.xml
<web-app> <servlet> <servlet-name>BeginnersBook</servlet-name> <servlet-class>DemoServlet</servlet-class> </servlet> <context-param> <param-name>uname</param-name> <param-value>ChaitanyaSingh</param-value> </context-param> <context-param> <param-name>email</param-name> <param-value>[email protected]</param-value> </context-param> <servlet-mapping> <servlet-name>BeginnersBook</servlet-name> <url-pattern>/context</url-pattern> </servlet-mapping> </web-app>
Output:
Methods of ServletContext interface
Here is the list of frequently used methods of ServletContext interface.
public String getInitParameter(String param): It returns the value of given parameter or null if the parameter doesn’t exist.
public Enumeration getInitParameterNames(): Returns an enumeration of context parameters names.
public void setAttribute(String name,Object object): Sets the attribute value for the given attribute name.
public Object getAttribute(String name):Returns the attribute value for the given name or null if the attribute doesn’t exist.
public String getServerInfo(): eturns the name and version of the servlet container on which the servlet is running.
public String getContextPath(): Returns the context path of the web application.
To get the full list of methods, refer the official documentation here.
Leave a Reply