Input hidden type
Hi,
How can I pass an array which is declred in Javascript to servlet.
I want to store the value of an array to a strinf (seperated by some character) and then store that string in to the hidden object of my jsp page, and then I want to read that value from servlet
So in short how to store an array to string and string hidden object og html form?
please help with sample code ASAP
Thanks
[426 byte] By [
ASH_2007a] at [2007-11-27 6:37:59]

# 1
Assuming you have a hidden field already in your form called myHiddenField and you have already made a string of your array in JavaScript, say myArrayAsAString:
function setHiddenField ( )
{
document.getElementById('myHiddenField').value = myArrayAsString;
}
Of course, you have to do the conversion of array to string yourself, it's easy enough :)
Something like:
myArrayAsAString = "";
for ( int i = 0; i < myArray.length; i++ )
{
myArrayAsAString += myArray[i] + "$"; //using '$' to separate
}
//here, you'll have to remove the last $ since that'll always get appended, so you'll have one extra. Or you can work your logic while extracting keeping in mind that the string will end in a $
# 2
I don't think it matters if there is a delimiter at the end, atleast not when using StringTokenizer.
String myArrayAsAString = "Foo$Bar$FooBar$";
StringTokenizer st = new StringTokenizer(myArrayAsAString,"$");
while (st.hasMoreTokens()) {
System.out.println("["+st.nextToken()+"]");
}
# 3
> I don't think it matters if there is a delimiter at
> the end, atleast not when using StringTokenizer.
> String myArrayAsAString =
> "Foo$Bar$FooBar$";
> StringTokenizer st = new
> StringTokenizer(myArrayAsAString,"$");
>while (st.hasMoreTokens()) {
>System.out.println("["+st.nextToken()+"]");
>}
>
Hmm, never used it :) Will definitely read up on this. But, she needs done in JavaScript not Java :D
[edit]
Just looked it up; the docs say:
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead
[/edit]
Her question is, that she needs to access a JavaScript array when the page is submitted to the JSP/servlet. And her idea is to convert the array to a delimited string and then put it into an HTML hidden field to access on the server-side with request.getParameter().
Message was edited by:
nogoodatcoding
# 4
Thank you very much for the help.
It was very useful.
But I have one more question.
I called the selected names (hidden field)in servlet with request.get parameter.
And I want to pass names in delete query.
But right now my string looks like = abc,bcd,cde.
So how can I pass them to my delete query.
please suggest..
Thank so much
-ASH
# 5
Well, first, I'm not sure what these values in the string are. Are they column names? Or are they the values that will indicate the record you want to delete? Like delete from mytable where somefield = abc? I'll assume it's the second case.
You know already that when you find a comma in your String, it means it's the end of one name and the start of another. So you need to extract the substring from the string. Read up on substring() and indexOf() methods.
You'll be doing something like:
String currentName = "";
while ( selectedNames.indexOf(",") != -1) //till there is atleast 1 ,
{
currentName = selectedNames.substring(0, selectedNames.indexOf(","));
selectedNames = selectedNames.substring(selectedNames.indexOf(",") + 1 );
System.out.println(currentName);
}
currentName = selectedNames;
System.out.println(currentName);
//consider abc,_def
//first you'll get currentName = abc
//then we set selectedNames to _def ( I've used underscore to indicate what'll happen with spaces! Try using String.trim() if you face problems)
//then the loop will break and selectedNames will contain the last name from teh list i.e. _def
Read up on the String methods; beyond this it'll really be spoonfeeding! :)
# 6
OKay, great!You solved my problem.Thanks so much for being so wonderful and helpfulGod bless.-Ash
# 7
Use [url=http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)]String#split()[/url].
String string = "foo,bar,meep";
String[] tokens = string.split(",");
for (String token : tokens) {
// handle each token
}
# 8
Hehe, you're welcome. But read up on the methods more. Go through the documentation, there's plenty on the net. You'll learn better that way instead of being told one method at a time at the forums.Good luck
# 9
> Use
> [url=http://java.sun.com/j2se/1.5.0/docs/api/java/lang
> /String.html#split(java.lang.String)]String#split()[/u
> rl].
>
> [code]String string = "foo,bar,meep";
> String[] tokens = string.split(",");
> for (String token : tokens) {
>// handle each token
> ode]
As always, BalusC points out a much better and efficient way of doing things and I learn along the way :)
# 10
Hello,i am having one more problem.If I just select one option its deleting it and working fine butIf you select multiple options it's just deleting the last option.Suggestion/help?Thanks
# 11
and oh, just your reply .Thanks for wishes!I appreciate it.Yeah I will follow the path you said.But can you answer my question above?plzz :-))
# 12
> If I just select one option its deleting it and
> working fine but
> If you select multiple options it's just deleting the
> last option.
Maybe you're using [url=http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)]ServletRequest#getParameter()[/url] instead of [url=http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html#getParameterValues(java.lang.String)]ServletRequest#getParameterValues()[/url]?
# 13
Hi,
Even If I use getParameter it's pulling all the values since it's once string only.
I have seperated names by comma.
So my string is like: str = abc, def, ghi; (selected option values)
Do i need some kind of loop or something coz when I pass it to my method (delete(Customer_Name))it takes only one name.So when it runs delete quesry only one name is getting deleted.
:-(
# 14
Actually, my probklem is:
Delete function is getting called once only:
I have used the code suggested by nogoodatcoding-author, which is helpful and working fine>
But I am calling delete function at the end:
String currentName = "";
while ( selectedNames.indexOf(",") != -1) //till there is atleast 1 ,
{
currentName = selectedNames.substring(0, selectedNames.indexOf(","));
selectedNames = selectedNames.substring(selectedNames.indexOf(",") + 1 );
System.out.println(currentName);
}
currentName = selectedNames;
System.out.println(currentName);
delete(currentName);
That's why it's deleting last value only.
IF tried to put in the while loop it does not work if there is only one value selected, and for multiple also only one value gets deleted.
I am confuse :-(
Message was edited by:
ASH_2007
# 15
String string = "foo,bar,meep";String[] tokens = string.split(",");for (String token : tokens) {delete(token);}Should
# 16
> String currentName = "";
>
> hile ( selectedNames.indexOf(",") != -1) //till there
> is atleast 1 ,
> {
> currentName = selectedNames.substring(0,
> selectedNames.indexOf(","));
> selectedNames =
> selectedNames.substring(selectedNames.indexOf(",") +
> 1 );
>
> System.out.println(currentName);
>
>
>
> urrentName = selectedNames;
> System.out.println(currentName);
> delete(currentName);
What I'd given you was just to give you some idea about how the extracting would work, it wasn't meant to be used directly in your code. You'll have to modify it accordingly.
What BalusC has suggested, putting it an array is what I do. And also, do use the split() method. It is a much better way and it'll handle cases with the ',' at the end and stuff like that.
If you must continue using the way you're doing already ( NOT recommended, it was for example purpose only ) then you should replace the 'System.out.println()' with the call to your delete() function. But I'd really suggest you work with what BalusC has given.
