By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,566 Members | 1,664 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,566 IT Pros & Developers. It's quick & easy.

Passing variable from javascript to servlet!!!

P: 9
Hi all,
I have one doubt,
can we pass a variable from javascript to servlet???

In Brief,
i am getting some values in javascript through jsp and the same values i
want in servlet. Ya i know i can get this values in servlet by using request.getParameter("");
But thise values are dynamically generated so i dont know how many values will be. so i am planning to put some array list so that i can forward this values to servlet.

but i can't get this array which is declared in javascript!! :( so any buddy can help me how to get this dynamically generated values in servlet!!!

Thanks in Advance!! :)
Nov 21 '08 #1
Share this Question
Share on Google+
7 Replies


10K+
P: 13,264
You would get an array (or values in a select) using request.getParameterValues instead.
Nov 21 '08 #2

P: 9
Hi,

But if i declare a array in javascript how can i get that array in servlet???
Is there any way to declare array in javascript/html and same we can use in servlet??
Nov 21 '08 #3

10K+
P: 13,264
You could just set the values to a hidden checkbox on the form and submit that.
Nov 21 '08 #4

P: 9
Hi,
Actually i have a dynamic table, i have add button to table which will add the new row to table, i want all the table values in servlet for my further use.
i used hidden select box,and i am adding all values to hidden selectbox, but problem is that i am getting only fist value, but if i give more than 2 or 3 rows, i am not receiving all values in servlet.

But if i use hidden checkbox how can i put all values in single checkbox value??
Nov 21 '08 #5

10K+
P: 13,264
The hidden select box approach should work as well. Post the code you used to populate the select.
Nov 21 '08 #6

P: 9
Hi,
I used this code to populate the select box.

Expand|Select|Wrap|Line Numbers
  1. var k;
  2.     for(k=1; k<=lastRow; k++)
  3.     {
  4.          var newOption = new Option(); 
  5.          var x=document.getElementById('selRow'+ k).selectedIndex;
  6.     var y=document.getElementsByTagName("option");
  7.  
  8.     alert(y[x].text+" In for loop has the index of: "+y[x].index);         
  9.  
  10.     newOption.text = y[x].text;
  11.     newOption.value = y[x].text;
  12.     document.table.ListBox4.options[len2] = newOption;
  13.     len2=len2+1;
  14.  
  15.     }
But some of the values are not inserting propertly!!
Nov 21 '08 #7

acoder
Expert Mod 15k+
P: 16,027
Line 6 (setting y) will cause problems because you're getting all the options in the document (which is increasing all the time in the loop). So put that statement outside the loop.
Nov 25 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.