| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
XyzWs JSP FAQ:
How to Use the methods of the request object extracting form data?
Printer-friendly version |
Mail this to a friend
|
Advertisement
|
How to Use the methods of the request object extracting form data?
The There are three common methods to use:
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. |