Select Html in JSP

0

I have on the page listing.jsp a list of users who take them out of a linkedlist

 <form name="formsendmessage" action="../mipagina.jsp"  method="POST">

        <select multiple> <%
                          LinkedList list = (LinkedList) session.getAttribute("emailusers");
                          for (int i = 0; i < list.size(); i++) { %>
                          <option name="listemails[]" value="<%=list.get(i) %>"><%=list.get(i) %></option>
                          <% }  %>  

                         </select>
                         <input type="submit" name="btnsendmessage" value="Send Answer">  
                </form>

And on my page.jsp

<% String [] listemails = request.getParameterValues("listemails[]");

            for (int i = 0; i < listemails.length; i++) {
                out.println(listemails[i]);
             } %>

Before when I showed them with <input type="check" name="listemails[]"> it worked perfectly, but not now, what's the problem?

    
asked by EduBw 13.10.2017 в 21:37
source

2 answers

0

The name of the select goes in the opening tag of this:

        <form name="formsendmessage" action="../mipagina.jsp"  method="POST">

            <select name="listemails[]"> <% /*Aquí va el name (nombre)*/
                  LinkedList list = (LinkedList) session.getAttribute("emailusers"); 
                  for (int i = 0; i < list.size(); i++) { %>
                  <option  value="<%=list.get(i) %>"><%=list.get(i) %></option>
                  <% }  %>  

            </select>
            <input type="submit" name="btnsendmessage" value="Send Answer">  
        </form>

To obtain the selected value of a select request.getParameter("nombreDelSelect"); is used and this only returns a String, that is, it does not need an array.

        <% String emailSeleccionado = request.getParameter("listemails[]");/*Manera correcta*/

              out.println(emailSeleccionado);

        %>
    
answered by 13.10.2017 / 23:55
source
0

Thanks, I already got it this way:

 <select multiple name="listemails[]">

                      <%
                          LinkedList list = (LinkedList) session.getAttribute("emailusers");
                          for (int i = 0; i < list.size(); i++) { %>
                          <option value="<%=list.get(i) %>"><%=list.get(i) %></option>
                          <% }  %>  

                         </select>

And to show the selected ones:

 String[] listemails = request.getParameterValues("listemails[]");

        for (int i = 0; i < listemails.length; i++) {
            out.println(listemails[i]);
         }

You need the getParameterValues. In case several values are returned. Thanks.

    
answered by 14.10.2017 в 01:09