Servlet context parameters and ServletContext interface

Context parameters:

Context parameters refer to the initialization parameters for all servlets of an application. <context-param> attribute is used to define a context parameter. <context-param> attribute has two main sub attributes <param-name> and <param-value>. The <param-name> contains the name of the parameter and <param-value> contains the value of the parameter.

ServletContext interface:

The servletContext interface is used to access the context parameters.

Commonly used methods of ServletContext interface:

1. getInitParameter(String name): Returns the value of the specified parameter if the parameter exists otherwise returns null. Syntax:

public String getInitParameter(String name)

2. getInitParameterNames(): Returns the names of context parameters as Enumeration if the servlet has context parameters otherwise returns an empty Enumeration. Syntax:

public Enumeration getInitParameterNames()

3. setAttribute(String name, Object object): Binds the specified object to the specified attribute name and puts this attribute in the application scope. Syntax:

public void setAttribute(String name,Object object)

4. getAttribute(String name): Returns the specified attribute if exists otherwise returns null. Syntax:

public Object getAttribute(String name)

5. removeAttribute(String name): Removes the specified attribute. Syntax:

public void removeAttribute(String name)

Example:

ContextParamExample.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This class is used to show the use of context parameters.
 * @author W3schools360
 */
public class ContextParamExample extends HttpServlet {
   private static final long serialVersionUID = 1L;
       
   //no-argument constructor.
    public ContextParamExample() {
        
    }

    protected void doGet(HttpServletRequest request, 
		HttpServletResponse response)
	              throws ServletException, IOException {
	response.setContentType("text/html"); 
    	PrintWriter out = response.getWriter();
    	//get ServletContext object.
    	ServletContext context=getServletContext();  
    	//get context parameter from ServletContext object.
    	String appUser = context.getInitParameter("appUser");
    	
    	out.print("Application User: " + appUser + "");
    	
    	out.close();
    }

}

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_2_5.xsd" id="WebApp_ID" version="2.5">
  
  <servlet>
    <servlet-name>ContextParamExample</servlet-name>
    <servlet-class>
        com.w3schools.business.ContextParamExample
    </servlet-class>
  </servlet>
  
  <context-param>
      <param-name>appUser</param-name>  
    <param-value>jai</param-value>  
  </context-param>
  
  <servlet-mapping>
    <servlet-name>ContextParamExample</servlet-name>
    <url-pattern>/ContextParamExample</url-pattern>
  </servlet-mapping>
  
</web-app>

 

Please follow and like us:
Content Protection by DMCA.com