Get all Init Parameters in Servlet (original) (raw)
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
<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>
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 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.