servlet

Get all Init Parameters in Servlet

With tutorial we shall show you how to get all Init Parameters in Servlet. Using init parameters you can specify servelar inportant aspects of your servlets that are going to be handled during requests service.

In order to do that you should:

  • Create public void init() function on your servlet.
  • Call getServletConfig().getInitParameterNames()
  • Use put(initParamName, initParamValue) to put parameters in your init parameter map.
  • In your doGet method use initParamsMap.entrySet().iterator() to get an Iterator an iterate through init parameters.

 
Let’s see the code snippets that follow:

package com.javacodegeeks.snippets.enterprise;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

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

public class GetAllInitParametersInServlet extends HttpServlet {

	private static final long serialVersionUID = -2128122335811219481L;

	private Map<String, String> initParamsMap = new HashMap<String, String>();

	public void init() throws ServletException {

		Enumeration<String> initParams = getServletConfig().getInitParameterNames();
		System.out.println(initParams + " initParams");

		while (initParams.hasMoreElements()) {

			String initParamName = initParams.nextElement();
			System.out.println(initParamName + " initParamName");
			String initParamValue = getServletConfig().getInitParameter(initParamName);

			initParamsMap.put(initParamName, initParamValue);

		}

	}

	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {

		PrintWriter out = res.getWriter();
		res.setContentType("text/plain");

		Iterator<Map.Entry<String, String>> iter = initParamsMap.entrySet().iterator();

		while (iter.hasNext()) {

			Map.Entry<String, String> entry = iter.next();
			String key = entry.getKey();
			String value = entry.getValue();

			out.write(key);
			out.write(" = ");
			out.write(value);
			out.write("n");

		}

		out.close();

	}

}

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"
  version="2.5">

	<display-name>JCG Snippets Web Project</display-name>

	<servlet>
		<servlet-name>JCG Snippets Application</servlet-name>
		<servlet-class>com.javacodegeeks.snippets.enterprise.GetAllInitParametersInServlet</servlet-class>
		<init-param>
			<param-name>myparam1</param-name>
			<param-value>myvalue1</param-value>
		</init-param>
		<init-param>
			<param-name>myparam2</param-name>
			<param-value>myvalue2</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>JCG Snippets Application</servlet-name>
		<url-pattern>/jcgservlet</url-pattern>
	</servlet-mapping>

</web-app>

URL:

http://myhost:8080/jcgsnippets/jcgservlet

Output:

myparam1 = myvalue1
myparam2 = myvalue2

 
This was an example on how to get all Init Parameters in Servlet.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button