Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

When using Select in a form, how do i send the multiple selections to a servlet?

Hi, I'm trying to figure out how to write a servlet that has a Select in which users can select multiple entries, and a
submit button that sends the values of the selected attributes to a servlet.  Here's a simple example i tried to make,
but it doesn't work (the servlet recieves only the first of the selected items).  Please help

;test.servlet
(servlet (age)
    (case age ((#null)
        {<html><head><title>Test Form</title></head>  
         <body>  
           <form method="get" action="test.servlet">  
             Select sereval of these numbers:
             <select name=age size=5 multiple>  
               <option value=10> 10 </option>
               <option value=20> 20 </option>
               <option value=30> 30 </option>
               <option value=40> 40 </option>
               <option value=50> 50 </option>
             <input type="submit">  
             </select>
           </form>  
         </body></html>})
     (else
       {<html><head><title>Test Form</title></head>  
        <body>  
           You selected
           [age]
        </body></html>})))

0
pavelmed
Asked:
pavelmed
  • 3
1 Solution
 
bubbledragonCommented:
use String array?

String[] arrValues[] = request.getParameterValues("age");
0
 
jessegivyCommented:
Hi bubbledragon,

The second set of brackets aren't required right?  Happens to the best of us, should look like this:

String[] arrValues = request.getParameterValues("age");
0
 
pavelmedAuthor Commented:
I'm not really sure what you meab by :
String[] arrValues = request.getParameterValues("age");
That looks like Java.
I am using JScheme for my servlet.  (http://www-swiss.ai.mit.edu/~jaffer/r4rs_toc.html)
Could you please show how and where to insert that in my code (because just putting it
in before [age] on the second to last line doesn't work).

Thanks a lot


-------------
Also, in the above code i accidentally put the  <input type="submit">   inside the select.  
So here's the corrected code:

;test.servlet
(servlet (age)
    (case age ((#null)
        {<html><head><title>Test Form</title></head>  
         <body>  
           <form method="get" action="test.servlet">  
             Select sereval of these numbers:
             <select name=age size=5 multiple>  
               <option value=10> 10 </option>
               <option value=20> 20 </option>
               <option value=30> 30 </option>
               <option value=40> 40 </option>
               <option value=50> 50 </option>
             <input type="submit">  
             </select>
           </form>  
         </body></html>})
     (else
       {<html><head><title>Test Form</title></head>  
        <body>  
           You selected
           [age]
        </body></html>})))
0
 
jessegivyCommented:
You just insert it in the page that's slotted to recieve the data wherever you need to access the data.  Usually there's some validation like:

if(request.getParameter("submitButton")!=null)
{
   String[] arrValues = request.getParameterValues("age");
   sql=conn.prepareStatement("UPDATE users SET age=? WHERE user_id=?")
   for(int i=0;i<srrValues.length;i++)
   {
       sql.setString(1,arrValues[i]);
       sql.setString(2,userID);
       sql.executeUpdate();
   }
}

...and keep in mind that the above code is merely a SIMPLE example, not intended for you to base your development on, just to get the idea across, I'm sure you're updating more than just age.  In fact, if you'd like a clearer code sample you'll need to post some of your logic i.e. what you're trying to do and what you're unclear about?

~Jesse
0
 
jessegivyCommented:
...sorry, and as to where, it's at the top of the page, or really, it doesn't exactly matter where it is.  I'm NOT familiar with JScheme, I'll check it out.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now