ServletConfig Example init-param read getInitParameter()


A Servlet may have some values needed for it’s execution or a Programmer may require some values to be fed to Servlet needed in the coding for execution. These values are specific to a particular Servlet and not required for all Servlets. For this, the deployment descriptor web.xml, comes with <init-param> tag and an example is given to read all <init-param> values and also a single one.

To read the <init-param> values, known as initialization parameters, we use ServletConfig interface from javax.servlet package.

  
    portnumber
    8888
  

The above code is an example entry in web.xml file. To read 8888 of <param-value>, it is used portnumber of <param-name>. portnumber and 8888 are used as key/value pairs in Servlet coding.

ServletConfig Example using getInitParameter() and getInitParameterNames() methods

Client file to invoke Servlet: InitParam.html


Would you like read initialization parameters from web.xml file sir?

The above HTML code includes just an hyper link to invoke ReadInitParamValues Servlet with alias name bharat.

Following is the web.xml entry for the Servlet:


  hello
  ReadInitParamValues
  
  
    pearlcity
    Hyderabad
  

  
    monument
    Charminar
  

  
    mangoCost
    250.5
  

  
    numberOfMangoes
    30
  
  



  hello
  /bharat

web.xml file can have any number of <init-param> tags.

Servlet file: ReadInitParamValues.java (of alias name bharat)

import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import javax.servlet.http.*;
import java.io.*;  
import java.util.Enumeration;

public class ReadInitParamValues extends HttpServlet   
{
  public void service( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException  
  {
    res.setContentType("text/html");
    PrintWriter pw = res.getWriter();
                                 // obtain an object of ServletConfig interface
    ServletConfig config = getServletConfig(); 
	
			         // to read all values
    pw.println("Reading all values:" + "
"); Enumeration e = config.getInitParameterNames(); while(e.hasMoreElements()) { String name = (String) e.nextElement(); // returns the String value = config.getInitParameter(name); // returns pw.println("
" + name + " : " + value ); } // to read one value (not all) and use in code String str1 = config.getInitParameter("mangoCost"); String str2 = config.getInitParameter("numberOfMangoes"); // parsing code double mc = Double.parseDouble(str1); int nom = Integer.parseInt(str2); pw.println("
Reading one value:" + "

"); pw.println("One mango cost: " + mc/nom); // to use getServletConfig() method pw.println("
Servlet Name: " + config.getServletName()); // prints of web.xml file. In this case hello pw.close(); } }

Before going into the explanation, let us see the new methods used in the code.

Following is the method of GenericServlet class used in the servlet code.

  public javax.servlet.ServletConfig getServletConfig();

Following are the methods of ServletConfig interface used in the servlet code.

  public abstract java.lang.String getServletName();
  public abstract java.lang.String getInitParameter(java.lang.String);
  public abstract java.util.Enumeration getInitParameterNames();

ServletConfig config = getServletConfig();

ServletConfig interface from javax.servlet package is used to read the initialization parameters of web.xml. Its object is returned by the getServletConfig() method of HttpServlet (inherited from GenericServlet). config is an object of ServletConfig.

Enumeration e = config.getInitParameterNames();

getInitParameterNames() method of ServletConfig interface returns an object of Enumeration. The object e of Enumeration contains the values of all <param-name> tags but not of <param-value> tags. The values of <param-value> tags are be obtained from <param-name> tags. This is what is done in the following loop.

    while(e.hasMoreElements())
    {
      String name = (String) e.nextElement();  // returns the  
      String value = config.getInitParameter(name);  // returns  
      pw.println("
" + name + " : " + value ); }

It is the same Enumeration interface we used to iterate collection classes. The nextElement() method of Enumeration returns an object of Object class and is type casted to String. The name variable contains the <param-name> value. The name variable is passed to getInitParameter(name) to get the value of its corresponding <param-value> value.

getInitParameterNames() returns all values and getInitParameter("mangoCost") returns only one value of corresponding <param-value> value. Observe the web.xml entry given earlier.

String str1 = config.getInitParameter("mangoCost");
String str2 = config.getInitParameter("numberOfMangoes");

getInitParameter(String) returns String object and is required to parse to use in arithmetic operations.

double mc = Double.parseDouble(str1);
int nom = Integer.parseInt(str2);

str1 representing mangoCost is parsed to double and str2 representing numberOfMangoes is parsed to int.

pw.println("<hr>Servlet Name: " + config.getServletName( ));

The <servlet-name> value of web.xml can be obtained with getServletName() of ServletConfig. See the output screen.


ima
Output screenshot of ServletConfig Example

Leave a Comment

Your email address will not be published.