For the whole web application the instance of a ServletContext is only one and the all other components may share (if required) this context.
For the whole web application the instance of a ServletContext is only one and the all other components may share (if required) this context.In this section we will read about the SevletContext.
ServletContext an interface is used for communicating the Servlet Container. ServletContext provides the various methods using which a Servlet container can share information among the other components. ServletContext can be used to get the file's MIME type, dispatch request etc. The ServletContext is created once for each web application by the ServletContainer whereas, the ServletConfig is created by the Servlet. ServletConfig object keeps the ServletContext object.
Benefits of ServletContext
Using the ServletContext you can share the information globally among all the Servlet. This global information can be shared by providing the value in web.xml file using the <context-param></context-param> tags. So, changing of information what you would like to share among Servlets doesn't put any effect on the Servlet.
Why ServletContext is useful?
To share the information among all the Servlet we can specify the value within the <context-param> </context-param> tag. Inside the "context-param" we can specify the <param-name> </param-name> and <param-value> </param-value>. Following code snippet will demonstrate you how we can map the context-param :
<servlet> <servlet-name>Mapping</servlet-name> <servlet-class>ContextMapping</servlet-class> </servlet> <context-param> <param-name>Email</param-name> <param-value>[email protected]</param-value> </context-param>
To get the context param value we can write the following code snippet inside the Servlet :
ServletContext context = getServletContext(); //ServletContext context = getServletConfig().getServletContext(); pw.println(context.getInitParameter("Email");
Web Application Initialization
ServletContext interface has various methods that are used for various other purposes. Some of the commonly used methods are as follows :
Example :
Here I am going to give a simple example which will demonstrate you about how you can get the ServletContext object and you can use it for various purposes. In this example you will see that we are getting the all the init parameters names and their values. For this we will use the getInitParameterNames(), and the getInitParameter() methods.
ServletContextExample.java
package net.roseindia; import java.io.*; import java.util.Enumeration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletContextExample extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); ServletContext context = getServletContext(); Enumeration parameters = context.getInitParameterNames(); String elements=""; String paraNames=""; out.println("Available Parameters Are :"); while(parameters.hasMoreElements()) { elements = (String)parameters.nextElement(); paraNames = context.getInitParameter(elements); out.println(elements+" : "+paraNames ); } } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>servletContext</display-name> <servlet> <servlet-name>contextExample</servlet-name> <servlet-class>net.roseindia.ServletContextExample</servlet-class> </servlet> <context-param> <param-name>name</param-name> <param-value>Deepak</param-value> </context-param> <context-param> <param-name>age</param-name> <param-value>26</param-value> </context-param> <context-param> <param-name>email</param-name> <param-value>[email protected]</param-value> </context-param> <context-param> <param-name>mobile</param-name> <param-value>123456789</param-value> </context-param> <servlet-mapping> <servlet-name>contextExample</servlet-name> <url-pattern>/ServletContextExample</url-pattern> </servlet-mapping> </web-app>
Output :
When you will execute the above example you will get the output as follows :
Ads