This section describes how to use multiple select option in JSP.
Multiple Select Option JSP
This section describes how to use multiple select option in JSP.
In a form sometimes you will be required to select the various options that you have to displayed them or manipulate with that value. This facility can be provided by the <select> tag of HTML that makes the drop down list with number of options. Values in a drop down list is inserted using the <option> sub tag of <select>. Let's come to our discussion how to display the values of the items selected in drop down list. Follow the example :
In the following example we will describe how to fetch the value of items selected in the drop down list. In this example at first I have created a JSP page named jspMultipleSelectOption.jsp where designed a form onto which taken a <select> tag and then put the value using its subtag <option>. If you want a default value setting you can use the attribute selected of <option> attribute. And taken a submit button for submitting the form. Then write the java codes, using the scriptlets to fetch the selected values from the drop down list and simply displayed them.
jspMultipleSelectOption.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>JSP Multiple Select Option</title> </head> <body> <h4>Select the color/s you like from the drop down list</h4> <form> <select name="color" multiple size="6"> <option>Pink</option> <option>Orange</option> <option>Yellow</option> <option>Blue</option> <option>Black</option> <option>Brown</option> <option>Gray</option> </select> <p><input type="submit" value="submit"></p> </form> <% String clrs[] = request.getParameterValues("color"); if(clrs != null) { %> <p><b>You have selected the following colors</b></p> <ul> <% for(int i=0; i<clrs.length; i++) { %> <li><%=clrs[i]%></li> <% } %> </ul> <% } %> </body> </html>
Output :
When you will execute the above JSP page you will get the output as follows :
1. At first a page will be displayed to you from where you can select the various options from the drop down list :
2. When you will select the options given in the drop down list as above and will click on submit button then the output will be as follows :
[ 0 ] Comments