Ives Rodriguez
Ives Rodriguez

Reputation: 35

convert parameter from multiselect checkbox of jsp page

<input type="checkbox" name="premium" value="HBO">HBO <br>
<input type="checkbox" name="premium" value="FOXP">FOX Película <br>
<input type="checkbox" name="premium" value="FOX">FOX + <br><br>
<input type="submit" value="cotizar" name="Cotizar" />
<br><br>

I'm trying to bring from an index.jsp the premium parameter of a checkbox, in a servlet of a client of a web service. I have to save it as a list but what I have tried has not worked

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String plan = request.getParameter("plan");
    String  PREMIUM = request.getParameter("premium");

How do I convert request.getParameter ("premium") into a list?

Upvotes: 0

Views: 653

Answers (2)

Srinu Chinka
Srinu Chinka

Reputation: 1491

You need to put all the checkbox inside of form then only multiple values will be sent to server.

HTML:

<form method="post">
    <input type="checkbox" name="premium" value="HBO">HBO <br>
    <input type="checkbox" name="premium" value="FOXP">FOX Película <br>
    <input type="checkbox" name="premium" value="FOX">FOX + <br><br>
    <input type="submit" value="cotizar" name="Cotizar" />
</form>

Servlet:

To get multiple values we need to use getParameterValues(), if you any value for the given parameter name it will give string array of values. If we have one value it will given string array with length of 1.

 String[] premiums = request.getParameterValues("premium");

If no value sent to server then above method will return null value.

Upvotes: 0

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

Try this:

String[] premiums = request.getParameterValues("premium");

Upvotes: 2

Related Questions