Share on Google+Share on Google+

Java ServletConfig Interface

In this tutorial, we will discuss about ServletConfig Interface.

Java ServletConfig Interface

In this tutorial, we will discuss about ServletConfig Interface.

ServletConfig Interface :

The servlet container uses a ServletConfig object to pass initialization information to the servlet. In general it is used to read the initialization parameters so whenever server wants to pass initialization data to a servlet, it creates a class which implements this interface.

This interface has following four methods -

  • getInitParameter(java.lang.String name) : It returns String holding value of the named initialization parameter. If parameter doesn't exist, it will return null.
  • getInitParameterNames() :  This method returns enumeration of String objects of all initialization parameter names. If servlet has no initialization parameters then it will return an empty Enumeration.
  • getServletContext() : It returns an object of the ServletContext in which the caller is executing.
  • getServletName() : It returns the name of the servlet instance.

Example :

import javax.servlet.*;
import javax.servlet.http.*;

public class ServletConfigExample extends HttpServlet {
	int counter;

	public void init() throws ServletException {
		String initValue = getServletConfig().getInitParameter("count");
		counter = Integer.parseInt(initValue);

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.println("You accessed this servlet " + counter + " times");

web.xml -

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



Output :



Posted on: October 5, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.