ServletConfig to access multiple initial parameter value

ServletConfig is ued to configure the servlet. We can configure values in the ServletConfig object and can access them later on. There can one or more values can be stored.

ServletConfig stores values in key values pair.

ServletConfig to access initial parameter value

All values are stored in key-value pairs in <servlet> the tag in the web.xml.

tag <init-param> is used to store key values inside <param-name> and <param-value> as below.

To Read all parameter names getServletConfig().getInitParameterNames() is used.

and to get the values of each name getServletConfig().getInitParameter(param) is used. The argument param is the name of the parameter.

Project Explorer

The project explorer for this project is as below.

Here we used Apache Tomcat 8.0.27.0 as the server and JDK 1.8 to develop this project

index.html

This file contains only a link on clicking a link it will call to the servlet.

Servlet Config get multiple parameters

web.xml

Web.xml file contains all configuration details in the Web project. The same thing can also be done using annotation.

Inside <servlet> tag <servlet-name> and <servlet-class>and <init-parm> are used. <servlet-name> specified the name of the servlet and used to map with URL mapping with <servlet-mapping>.

<servlet-class> specified the fully qualified class name of the servlet.

<init-param> is used to specify the initial parameters of the servlet. The init-param stores the <param-name> and <param-values> to store the name and value parameter of the servlet.

There can be many initial parameters for any servlet.

For example, we have taken four initial parameters to use in the servlet.

ServletConfigMulParam.java

This Servlet reads all servlet config names using getServletConfig().getInitParameterNames();

This will return Enumeration that is stored in params.

params will be iterated over the loop and all param names and values are printed.

 

 

Result

Index pages show a heading and a link.

Onclick on the link it will go to the next page where all parameters are read in the servlet and shown on the page.

 

Here we discussed ServletConfig to access multiple initial parameter value hope it will helpful for you all.