SE452: Processing Form Elements [10/20] ![]() ![]() ![]() |
To process the new form elements with multiple available values
, you can use the following methods of the ServletRequest
Enumeration getParameterNames()
Returns an Enumeration of the parameter names in this request
String[] getParameterValues(String name)
Returns a String array of values for a given Parameter name. This method is needed if you have parameters with multiple results, like a Select input.
Returns null if the Parameter doesn't exist
Here's an example that can be used to process the earlier form:
package se452.forms;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author Matthew Wright
* @version 1.0
*/
public class SurveyServlet extends HttpServlet {
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Survey Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<table>");
/* get the parameters and list them, with their values */
Enumeration parameters = request.getParameterNames();
if(parameters.hasMoreElements()) {
out.println("<tr><th>Parameter Name</th><th>Parameter Value</th></tr>");
}
while(parameters.hasMoreElements()) {
String parameter = (String)parameters.nextElement();
// get the parameter values
String[] values = request.getParameterValues(parameter);
if(values != null) {
for(int i = 0; i < values.length; i++) {
out.println("<tr><td><b><font color=\"blue\">" + parameter + "</font></b></td>" +
"<td>" + values[i] + "</td></tr>");
}
}
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
out.close();
}
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
}