Obtain the value submitted by multiple select in the form in JSP

Source: Internet
Author: User
In JSP, the value submitted by the multiple SELECT statement in the form is obtained. In JSP, the value submitted by the multiple SELECT statement in the form is 10.0102.

Add the multiple = "multiple" attribute to the select tag to convert the drop-down list to the multi-select list ), however, in JSP and Servlet, requests are commonly used to obtain form values. getparameter ("value") obtains only the first selected value in the multiple-choice list. After querying the data, it turns out that httpservletrequest has another method getparametervalues (string Key ), the returned value is a string array, which stores the selected values in the multiple-choice list.

That is to say, the submitted values of the multiple-choice control in the form in JSP and Servlet are obtained through the getparametervalues method, such as the checkbox list (check box list). The sample code is as follows:
<Form action = "index. jsp" method = "Post">
<Select name = "value" multiple = "multiple">
<Option value = "val1"> value 1 </option>
<Option value = "val2"> value 2 </option>
<Option value = "val3"> value 3 </option>
<Option value = "val4"> value 4 </option>
<Option value = "val5"> value 5 </option>
</SELECT>
<Input type = "Submit" value = "Submit">
</Form>

<%
Out. Write ("String [] selected = request. getparametervalues ("value ");
If (selected! = NULL ){
For (INT I = 0; I <selected. length; I ++ ){
Out. Write ("<p>" + selected [I] + "<p> ");
}
} Else {
Out. Write ("<p> no value selected <p> ");
}
%>

1) initial status, not selected

2) Assume that 2, 3, 4 are selected.

 

3) Result:

4) Select

 

5) Result:

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.