Servlet ServletConfig

In this tutorial you will learn about the Servlet ServletConfig and its application with practical example.

Servlet ServletConfig Interface

During initialization a servlet container uses a servlet configuration object so that it can pass information. In other word this object can be used to get configuration information from web.xml file. Now suppose we have change the information in web.xml file so we don’t need to change the servlet.

Get Object of ServletConfig

The getServletConfig() method is used to returns the object of ServletConfig.

ServletConfig Interface Methods

public string getInitParameter(String name):- This method is used to get the value of initialization parameter with the given name.

public Enumeration getInitParameterNames():- This method is used to returns the names of servlet’s initialization parameter as an Enumeration of String object.

public String getServletName(): This method returns the name of Servlet.

public ServletContext getServletContext(): This method is used to returns a reference to the ServletContext in which the caller is executing.

Initializing parameter in web.xml file

Syntax:-

Example:-

Let’s understand this by an example. Create Servlet file as follow.

ServletConfiDemo.java :-

In this file we have created object of ServletConfig interface and with the help of this object we got the init parameter. Check below code.

Web.xml :-

In the web.xml file we have set our init parameter . Follow below code.

Output :-

Let’s see the output of this simple task. Check below image.

image001

In this tutorial we have learn about the Servlet ServletConfig and its application with practical example. I hope you will like this tutorial.