Harish
Harish

Reputation: 1647

server side get multiple select values

A continuation of the previous question: multiple select

Is there a way to get the selected values in jsp(server side) ?

Upvotes: 1

Views: 16690

Answers (2)

BalusC
BalusC

Reputation: 1109132

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.

Upvotes: 0

David Rabinowitz
David Rabinowitz

Reputation: 30448

The following call returns an array of strings:

String[] values = request.getParameterValues("the-select-name");

Upvotes: 9

Related Questions