Web Development with JavaServer Pages

Скачать в pdf «Web Development with JavaServer Pages»


The next data collection method, reportParameters(), lists the request parameters that accompanied the original page request. Note that the HttpServletRequest class does not distinguish between parameters supplied in the URL via a query string and those provided in the body of an HTTP post request. In fact, both


may be present in the same request, and will be combined into one overall set of parameters. If values for the same parameter are provided multiple times, all of the values are stored. In such a case, the first value supplied for a parameter takes precedence, and parameter values set in the URL take precedence over those set in the body of the request. The code for this method is as follows:


public void reportParameters (StringBuffer buffer, HttpServletRequest req) { Enumeration names = req.getParameterNames(); if (names.hasMoreElements()) { buffer.append(«Parameters:n»); while (names.hasMoreElements())    {


String name = (String) names.nextElement();


String[] values = req.getParameterValues(name); for (int i = 0; i < values.length; ++i)    {


bu ffer.append(»    «);


buffer.append(name); buffer.append(» = «); buffer.append(values[i]); buffer.append(‘n’);


}


}


}


}


Here, the getParameterNames () method is called to obtain an enumeration of all of the parameters known to the request. If there is at least one parameter present, the next step is to print the name of each parameter, and its values. Since one parameter may have multiple values, a nested iteration loop is required to iterate over all of the values returned by the getParameterValues() method.


After listing the request parameters, the next step is to list the request headers, using the following reportHeaders() method:

Скачать в pdf «Web Development with JavaServer Pages»