Each Servlet application allows you to configure and retrieve parameters via web.xml . You can specify initial parameter name/value pairs using <init-param> tag. In this example we will learn how to retrieve configuration information from the web.xml file.
To retrieve initial parameters, you need the ServletConfig object passed by the servlet container to the servlet. After you get the ServletConfig object, you can use either getInitParameterNames and strong>getInitParameter methods.
- The getInitParameterNames does not take an argument and returns an Enumeration containing all the parameter names in the ServletConfig object.
- The getInitParameter takes a String containing the parameter name and returns a String containing the value of the parameter.
Since the servlet container passes a ServletConfig object to the init method, we will be writing our logic in the init() method itself. Here we will be configuring two initial parameters, the first parameter is “admin” whose value will be “JavaInterviewPoint” and the second parameter is “admin_contact” and the value for the parameter is “123456789”. We will be implementing Servlet Interface to achieve this.
ServletConfigExample.java
Lets see the below Servlet Example which implements the Servlet Interface
package com.javainterviewpoint; import java.io.IOException; import java.util.Enumeration; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class ServletConfigExample implements Servlet { public void init(ServletConfig config) throws ServletException { Enumeration parameters = config.getInitParameterNames(); while (parameters.hasMoreElements()) { String parameter = (String) parameters.nextElement(); System.out.println("Parameter name : " + parameter); System.out.println("Parameter value : " + config.getInitParameter(parameter)); } } public void destroy() { } public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { } public String getServletInfo() { return null; } public ServletConfig getServletConfig() { return null; } }
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"> <display-name>ServletsInterfaceExample</display-name> <servlet> <servlet-name>ServletConfigExample</servlet-name> <servlet-class>com.javainterviewpoint.ServletConfigExample</servlet-class> <init-param> <param-name>admin</param-name> <param-value>JavaInterviewPoint</param-value> </init-param> <init-param> <param-name>admin_contact</param-name> <param-value>123456789</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>ServletConfigExample</servlet-name> <url-pattern>/ServletConfigExample</url-pattern> </servlet-mapping> </web-app>
Output
To run our ServletConfigExample application hit on the below url
http://localhost:8080/ServletTutorial/ServletConfigExample
We will get the below output displayed in the browser
Parameter name : admin Parameter value : JavaInterviewPoint Parameter name : admin_contact Parameter value : 123456789
Leave a Reply