Getting Init Parameter Names in Servlet

In this example we are going to retreive the init paramater values which we have configured in the web.xml file.

The example given below will help you to get the parameter values which are given into the web.xml file. Whenever a servlet is deployed on server,after starting the server deployment descriptor file i.e. web.xml file is being read by the container. A name/value pair is created for the ServletConfig object by the Container and is used for passing information to servlet when initialized.To accomplish this following methods will be used.

In this example we are going to retreive the init paramater values which we have configured in the web.xml file.

The example given below will help you to get the parameter values which are given into the web.xml file. Whenever a servlet is deployed on server,after starting the server deployment descriptor file i.e. web.xml file is being read by the container. A name/value pair is created for the ServletConfig object by the Container and is used for passing information to servlet when initialized.To accomplish this following methods will be used.



getServletConfig() : This method gives back an object of servlet config. Any parameters that are to be initialized and the configuration has to be started up are contained by this method.

getInitParameter() : This method gets the value of the argument which has to be initialized the name givenby.

getInitParameterNames() : This method takes no argument it gives back names of the servlet's parameters that is to be initialized in the term of Enumeration of String object's.

To retrieve all the values of the init parameter use method getInitParameterNames() which will return the Enumeration of the init parameters.

The code of the program is given below:

GetInitParameter.java

package com.jwt.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetInitParameter extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter pw = response.getWriter();
		pw.print("Following are the Init Parameters : ");
		Enumeration enumeration = getServletConfig().getInitParameterNames();
		while (enumeration.hasMoreElements()) {
			pw.print(enumeration.nextElement() + " ");
		}
		pw.println("\nThe email address is "
				+ getServletConfig().getInitParameter("Email"));
		pw.println("The address is "
				+ getServletConfig().getInitParameter("Address"));
		pw.println("The phone no is "
				+ getServletConfig().getInitParameter("PhoneNo"));
	}
}

web.xml file of this program:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

	<servlet>
		<servlet-name>init</servlet-name>
		<servlet-class>com.jwt.servlet.GetInitParameter</servlet-class>
		<init-param>
			<param-name>Email</param-name>
			<param-value>mrajnitro@yahoo.co.in</param-value>
		</init-param>
		<init-param>
			<param-name>Address</param-name>
			<param-value>OMR Chennai</param-value>
		</init-param>
		<init-param>
			<param-name>PhoneNo</param-name>
			<param-value>99178766787</param-value>
		</init-param>

	</servlet>

	<servlet-mapping>
		<servlet-name>init</servlet-name>
		<url-pattern>/initservlet</url-pattern>
	</servlet-mapping>

</web-app>

Output in Browser:

Servlet Database Example






comments powered by Disqus