ServletConfig Example init-param read getInitParameter()

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.

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

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:

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

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

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.

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

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.

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 Reply

Your email address will not be published. Required fields are marked *