Servlet Init parameters and ServletConfig interface

Init parameters:

Init parameters refer to the initialization parameters of a servlet or filter. <init-param> attribute is used to define an init parameter. <init-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.

ServletConfig interface:

The ServletConfig interface is used to access the init parameters.

Methods of ServletConfig interface:

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

public String getInitParameter(String name)

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

public Enumeration getInitParameterNames()

3. getServletContext():Returns an instance of ServletContext.

public ServletContext getServletContext()

4. getServletName(): Returns the name of the servlet.

public String getServletName()


import javax.servlet.ServletConfig;
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 init parameters.
 * @author W3schools360
public class InitParamExample extends HttpServlet {
	private static final long serialVersionUID = 1L;
        //no-argument constructor
        public InitParamExample() {

      protected void doGet(HttpServletRequest request, 
		HttpServletResponse response)
	          throws ServletException, IOException {
    	PrintWriter out = response.getWriter();
    	//get ServletConfig object.
    	ServletConfig config=getServletConfig(); 
    	//get init parameter from ServletConfig object.
    	String appUser = config.getInitParameter("appUser");
    	out.print("Application User: " + appUser + "");


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