ServletConfig Interface in Java

Introduction

This article explains the ServletConfig interface in Java. The Netbeans IDE is used for sample example.

What ServletConfig is

This object is provided by the Servlet vendor, it provides configuration details from the web.xml file to servlets.

Servlets don't need to modify it when there is a change in the web.xml file. So it makes web applications easy to manage when any specific content is modified from time to time.

Advantages

The main advantage is that if there is any modification in our web.xml file then we don't need to modify the servlet file.

Some public methods of the ServletConfig interface are:

  • Enumeration getInitParameterNames()

          Returns an enumeration for all paramters.

  • String getServletName()

          Returns the servlet name.

  • ServletContext getServletName()String

          Returns the value of the specified parameter.

  • String getInitParameter(String sname)

          Returns the value of a specified parameter.

  • ServletContext getServletContext();

          Returns the ServletContext object.

How to create the object of ServletConfig

javax.servlet.Servlet provides a method named getServletConfig(). This method returns the object of the ServletConfig interface.

Syntax

public ServletConfig getServletConfig();

Example

ServletConfig object=getServletConfig();

What the Initialization parameter (IP) is

IP refers to information provided by the application programmer in the web.xml file. This information is made available to the servlet by the web server.

They are of the following two types:

  1. Servlet Specific.
  2. Application Specific.

1. Servlet Specific

These IPs provide information to a specific servlet at the application.

2. Application Specific

These IPs provide uniforms to all the servlets of the app.

A Servlet specific and application specific IP are defined in the web.xml as follows.

<web-app>
<init-param>
<context-param>
  ........

<param-name>parametername</param-name>
<param-value>parametervalue</param-value>
</init-param>
  ..........

<servlet>
<servlet-name>Name</servlet-name>
<servlet-class>Value</servlet-class>
</init-param>
</servlet>
</web-app>

Example of ServletConfig

In this example, we are creating a servlet that gets the parameter from the web.xml file then deploys it on the server.

We need to use the following procedure.

Step 1

Open the NetBeans IDE.

Fig-1.jpg

Step 2

Choose "Java web" -> "web application" as shown below.

Fig-2.jpg

Step 3

Now type your project name as "ServletConfigDemo" and click on "Next" as shown below:

Fig-3.jpg

Step 4

Now choose your server and Java version installed on your system and then click on "Finish" as shown below.

Fig-4.jpg

Step 5

Now delete your default index.jsp file and create a new index.html file as shown below.

Fig-5.jpg

Step 6

Now type your file name as "index" as shown below.

Fig-6.jpg

Step 7

Now provide the following code over there.

<!DOCTYPE html>

<html>

    <head>

        <title>ServletConfigDemo</title>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <meta name="viewport" content="width=device-width">

    </head>

    <body>

    <center><h1>Click on below link to get oracle-driver name</h1></center><br>

    <a href="ServletConfigDemo">Click Here</a>

</body>

</html>

Fig-7.jpg

Step 8

Now create a servlet named "ServletConfigDemo" and enter the following code into it.

ServletConfigDemo.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class ServletConfigDemo extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/html");

        PrintWriter out = response.getWriter();

        ServletConfig config = getServletConfig();

        String driver = config.getInitParameter("driver");

        out.print("Oracle Driver is: " + driver);

        out.close();

    }

}

Fig-8.jpg

Step 9

Now replace the code of your default web.xml file with the following code.

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <servlet>

        <servlet-name>ServletConfigDemo</servlet-name>

        <servlet-class>ServletConfigDemo</servlet-class>

        <init-param>

        <param-name>driver</param-name>

        <param-value>oracle.jdbc.driver.OracleDriver</param-value>

        </init-param>

 

    </servlet>

    <servlet-mapping>

        <servlet-name>ServletConfigDemo</servlet-name>

        <url-pattern>/ServletConfigDemo</url-pattern>

    </servlet-mapping>   

</web-app>

Fig-9.jpg

Step 10

Now our application is ready.

Right-click on the project menu then select "Run". The following output is generated.

Fig-10.jpg

Now click on the link provided.

Fig-11.jpg


Similar Articles