Easy learning with example program codes

Servlet context parameters and ServletContext interface

Context parameters:

Context parameters refers 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:

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 parameter exist otherwise return null.

Syntax: public String getInitParameter(String name)

2. getInitParameterNames(): Returns the names of context parameters as Enumeration if 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 put this attribute in application scope.

Syntax: public void setAttribute(String name,Object object). 

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

Syntax: public Object getAttribute(String name)

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

Syntax: public void removeAttribute(String name).


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 codesjava
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 {
    	PrintWriter out = response.getWriter();
    	//get ServletContext object.
    	ServletContext context=getServletContext();  
    	//get context parameter from ServletContext object.
    	String appUser = context.getInitParameter("appUser");
    	out.print("<h1>Application User: " + appUser + "</h1>");


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" 
id="WebApp_ID" version="2.5">


servlet example 8
Download this example.
Next Topic: Servlet Hello World Example using annotation with example.
Previous Topic: Servlet Init parameters and ServletConfig interface with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki