Show Parameter In Servlet
In this section, you will learn how to send and put all parameter names into the table. The following program uses two methods, which is described below.
Code Description:
Here, in this example, you will also see the use of getParameterNames() method and getParameterValues() method. The logic of the program will be written inside the doGet() method which takes two arguments, first is HttpServletRequest interface and the second one is the HttpServletResponse interface and this method can throw ServletException First, it looks up all the parameter names via the getParameterNames() method of HttpServletRequest. This returns an Enumeration. Next, it loops down the Enumeration in the standard manner, using hasMoreElements() method to determine when to stop and using nextElement to get each entry. Since nextElement returns an Object, it casts the result to a String and passes that to getParameterValues, yielding an array of Strings. If that array is one entry long and contains only an empty string, then the parameter had no values, and the servlet generates an italicized "No Value" entry. The array takes more then one entry long, then the parameter had multiple values and they are displayed in a table list. Otherwise the one main value is just placed into the table.
getParameterNames(): getParameterNames() is a method. This is the method that returns the parameter names for the request as an enumeration of string .
getParameterValues: getParameterValues() is a method. This is the method that returns the values of the specified parameter for the request as an array of strings or null if the named parameter does not exit.
Html file for this program:
<html>
|
Here is the code of this program:
import java.io.*;
|
xml file for this program:
<?xml version="1.0" encoding="ISO-8859-1"?> <!--<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> --> <web-app> <servlet> <servlet-name>amar</servlet-name> <servlet-class>ShowParameterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>amar</servlet-name> <url-pattern>/ShowParameterServlet</url-pattern> </servlet-mapping> </web-app> |
Output of this program.