Home Tutorial Servlet Java ServletConfig Interface


Share on Google+Share on Google+
Java ServletConfig Interface
Posted on: October 5, 2012 at 12:00 AM
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 java.io.*;
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="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">



Output :


Related Tags for Java ServletConfig Interface:

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: October 5, 2012

Recommend the tutorial

Advertisements Advertisements