A continuation of the previous question: http://stackoverflow.com/questions/1905534/multiple-select
Is there a way to get the selected values in jsp(server side) ?
A continuation of the previous question: http://stackoverflow.com/questions/1905534/multiple-select
Is there a way to get the selected values in jsp(server side) ?
The following call returns an array of strings:
String[] values = request.getParameterValues("the-select-name");
I've read your previous question and that piece of Javascript is fairly superflous if all you want is just to submit all selected values. Just doing a getParameterValues()
on the <select>
field name is enough. And you normally do that in a Servlet, not in a JSP.
<form action="myservlet" method="post">
<select name="myselect" multiple>
<option value="value1">label1</option>
<option value="value2">label2</option>
<option value="value3">label3</option>
</select>
<input type="submit">
</form>
servlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] selected = request.getParameterValues("myselect");
// Handle it.
// Now show the "result.jsp".
request.getRequestDispatcher("result.jsp").forward(request, response);
}
If you want to display the selected values in result.jsp
, then use JSTL c:forEach:
<c:forEach items="${param.myselect}" var="selected">
Selected item: ${selected}<br>
</c:forEach>
More about servlets in Java EE tutorial part II chapter 4.