How to Use the methods of the request object extracting form data?
The request
object retrieves the values that the client browser passed to the server during an HTTP request such as headers
, cookies
or parameters
associated with the request. Among the most common use of the request
object is to obtain parammeter or query string values.
There are three common methods to use:
-
getParameter(String param)
returns the value of the specified parameter as a string if it exists or null if it doesn't. Often, this is the value defined in the Value attribute of the control in the HTML page or JSP. -
getParameterValues(String param)
returns an array of String objects containing all of the values that the given request parameter has or null if the parameter doesn't have any values.To retrieve multiple values for one parameter name, you can use the getParameterValues method as illustrated by the second example. This method is useful for controls like list boxes that allow multiple selections. After you use the getParameterValues method to return an array of String objects, you can use a loop to get the values from the array.
-
getParameterNames()
returns an Enumeration object that contains the names of all the parameters contained in the request. If the request has no parameters, the method returns an empty Enumeration object.To get the names of all the parameters sent with a request, you can use the getParameterNames method to return an Enumeration object that contains the names. Then, you can search through the Enumeration object to get the parameter names, and you can use the getParameter method to return the value for each parameter name.
You can play the following html and JSP to see how they work:
<html>
<body>
<form action="actionprocess.jsp" method="post">
<select multiple size="4" name="selectGroup">
<OPTION selected value="Component_1_a">Component_1</OPTION>
<OPTION selected value="Component_1_b">Component_2</OPTION>
<OPTION>Component_3</OPTION>
<OPTION>Component_4</OPTION>
<OPTION>Component_5</OPTION>
</select>
<br>
<input type="radio" name="radioGroup" value="Milk"> Milk<br>
<input type="radio" name="radioGroup" value="Butter"> Butter<br>
<input type="radio" name="radioGroup" value="Cheese"> Cheese<br>
<input type="submit" value="Send"> <input type="reset">
</form>
</body>
</html>
and the actionprocess.jsp
<%@ page import="java.util.*" import="java.io.*"%>
<%
out.println("<b>Select Box in the Form</b><hr>");
out.print("Use getParameter(): <br>");
String selectStr = request.getParameter("selectGroup");
out.println(selectStr + "<br>");
out.println("<br>Use getParameterValues() to get list of value :<br>");
String[] selectStrs = request.getParameterValues("selectGroup");
if (selectStrs != null ) {
for (int i = 0; i < selectStrs.length; i++){
out.println(selectStrs[i] + "<br>");
}
}
else {
out.println("null<br>");
}
out.println("<br><b>Radio in the Form</b><hr>");
out.print("Use getParameter(): <br>");
String radioStr = request.getParameter("radioGroup");
out.println(radioStr + "<br>");
out.println("Use getParameterValues() to get list of value :<br>");
String[] radioStrs = request.getParameterValues("radioGroup");
if (radioStr != null) {
for (int i = 0; i < radioStrs.length; i++){
out.println(radioStrs[i] + "<br>");
}
}
else {
out.println("null<br>");
}
out.println("<br><b>Use getParameterNames() and getParameter()</b> :");
out.println("<hr>");
Enumeration parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()){
String parameterName = (String) parameterNames.nextElement();
String parameterValue = request.getParameter(parameterName);
out.println(parameterName + " has value " + parameterValue + "<br>");
}
%>
You can use the getParameter method to return the value of the selected radio button in a group or the selected item in a combo box. You can also use it to return the value of a selected check box or independent radio button, but that value is null if it isn't selected.
If an independent radio button or a checkbox doesn't have a Value attribute, this method returns "on" if the control is selected or null if it isn't.
In most cases, the getParameter method returns the value of the parameter. For a textbox, that's usually the value entered by the user. But for a group of radio buttons or a combo box, that's the value of the button or item selected by the user.
For checkboxes or independent radio buttons that have a Value attribute, the getParameter method returns that value if the checkbox or button is selected and a null value if it isn't. For checkboxes or independent radio buttons that don't have a Value attribute, though, the getParameter method returns an "on" value if the checkbox or button is selected and a null value if it isn't. This is illustrated by the first example in this figure.
Most Recent jsp Faqs
- How to get the real requested URI from inside the error page?
- Can I define a method in a JSP page?
- What is the difference between request.getParameter() and request.getAttribute()?
- What is difference between page and pageContext in JSP pages?
- How to config a JSP file in web.xml?
- What is the difference between jsp:forward and response.sendRedirect?
- How to retrieve values from HTML form input elements in JSP?
Most Viewed jsp Faqs
- How to config a JSP file in web.xml?
- How to retrieve values from HTML form input elements in JSP?
- What is the difference between request.getParameter() and request.getAttribute()?
- How to use comments in JSP pages?
- What is difference between page and pageContext in JSP pages?
- How to disable browser caching for a specific JSP?
- How to display all the session variables in an HTML page?