Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Servlets Servlet Context

Related Tutorials


 
 

Share on Google+Share on Google+

Servlet Context

Advertisement
For the whole web application the instance of a ServletContext is only one and the all other components may share (if required) this context.

Servlet 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?

  • ServletContext object is useful because it act as an interface between Container and Servlet.
  • Using ServletContext object you can get the configuration information.
  • Using ServletContext object you can set, get or remove the attribute from web.xml file.
  • ServletContext object can allow for the inter-application communication.

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>admin@roseindia.net</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

  • When the application is deployed to the web container it first reads the deployment descriptor file i.e. web.xml file and then it creates the name-value pair of each <context-param> tag.
  • Then it creates the new instance of ServletContext.
  • Then Servlet Container provides the reference of ServletContext to the context init parameters.
  • Now the other components such as JSP, as well as other Servlets that are part of the same application can access the ServletContext.

ServletContext interface has various methods that are used for various other purposes. Some of the commonly used methods are as follows :

  • Object getAttribute(String name) : This method is used to get the Servlet container attribute.
  • Enumeration getAttributeNames() : This method is used to get the available names of Servlet container attribute.
  • String getInitParameter(String name) : This method is used to get the parameter value for the specified parameter name.
  • Enumeration getInitParameterNames() : This method is used to get the available parameter names.
  • String getMimeType(String file) : This method is used to get the MIME of the specified file.
  • String getContextPath() : This method is used to find out the context path of web application.
  • String getRealPath(String path) : This method is used to find out the real path of the specified path.
  • RequestDispatcher getRequestDispatcher(String path) : This method is used to find out the request dispatcher of the specified path.
  • void setAttribute(String name, Object object) : This method is used to set the attribute as an Object with a given name of ServletContext.
  • void removeAttribute(String name) : This method is used to delete the attribute associated with the specified attribute name in the ServletContext.

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>deepak@roseindia.net</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 :

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 27, 2013

Related Tutorials

Discuss: Servlet Context  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com