ServletConfig getInitParameterNames

ServletConfig getInitParameterNames


Posted in : Java Posted on : June 20, 2011 at 11:42 AM Comments : [ 0 ]

In this section we will discuss how can you get the parameter names in java servlet.

ServletConfig getInitParameterNames

In this section we will discuss how can you get the parameter names in java servlet.

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

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.

Example :  GetInitParameter.java

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class GetInitParameter extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("A Student have the following record : ");
Enumeration enm = getServletConfig().getInitParameterNames();
while (enm.hasMoreElements()) {
out.print(enm.nextElement() + " ");
}
out.println("");
out.println("\nEnr. No. : "
+ getServletConfig().getInitParameter("enrNo"));
out.println("Name : "
+ getServletConfig().getInitParameter("name"));
out.println("Programme : "
+ getServletConfig().getInitParameter("prg"));
out.println("Aaddress : "
+ getServletConfig().getInitParameter("add"));
out.println("Phone no : "
+ getServletConfig().getInitParameter("phNo"));
}

}

web.xml

<web-app>
<servlet>
<init-param>
<param-name>enrNo</param-name>
<param-value>102569638</param-value>
</init-param>

<init-param>
<param-name>name</param-name>
<param-value>Bipul</param-value>
</init-param> 

<init-param>
<param-name>prg</param-name>
<param-value>MCA</param-value>
</init-param>

<init-param>
<param-name>add</param-name>
<param-value>Rohini</param-value>
</init-param>

<init-param>
<param-name>phNo</param-name>
<param-value>9013278579</param-value>
</init-param>
<servlet-name>GetInitParameter</servlet-name>
<servlet-class>GetInitParameter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetInitParameter</servlet-name>
<url-pattern>/GetInitParameter</url-pattern>
</servlet-mapping>
</web-app>

Output :

When you will execute this program you will find the following output.

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics