Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Passing Javascript array to servlet

Reply
Thread Tools

Passing Javascript array to servlet

 
 
Garg
Guest
Posts: n/a
 
      02-08-2007
Hi All,

I am facing one problem if you are having any solution please tell me.

I have to pass an array from javascript to servlet. for this i created
one array and pass that through submitting the form with post method
and i am using request.getParameterValues to get that array. But i am
getting values in the first position of that array and that also comma
separated so that also of no use for me.

Is there any way to pass the value to the Servlet?

Thanks
Tarun Garg

 
Reply With Quote
 
 
 
 
Erwin Moller
Guest
Posts: n/a
 
      02-08-2007
Garg wrote:

> Hi All,
>
> I am facing one problem if you are having any solution please tell me.
>
> I have to pass an array from javascript to servlet. for this i created
> one array and pass that through submitting the form with post method
> and i am using request.getParameterValues to get that array. But i am
> getting values in the first position of that array and that also comma
> separated so that also of no use for me.
>
> Is there any way to pass the value to the Servlet?
>
> Thanks
> Tarun Garg


Hi Garg,

You can do many things, but I think the easiest is simple define your own
datastructure, or better said: data encodingscheme.

A simple example:
Suppose you have (in JS) an array that contains only numbers, and want to
pass that.
convert:
Array(1,4,67,897,1254);
to a string:
"1,4,67,897,1254"
and put that string into a hidden field (or a normal textfield, or whatever
part of your form that can hold a string).

When the servlet receives the Form, you can simple get the string, split it
by ',' and create a new array in Java that you populate with the seperate
values.

If you have to send more complex information, eg if you JS array contains
characters and number, you'll have to encode the characters before adding
them to a string.
eg:
If you have this:
Array(1, 'bla,di,bla', 45)
You'll have top make sure the , in 'bla,di,bla' are encoded so you can split
the string when it arrives.

I never had a real-life situation where I couldn't encode an Array in the
simple way I just described, BUT: If your array is really complex, and
contains arrays itself, you better start looking at other solutions, maybe
JSON (I never used that, so maybe I talk nonsense).


Good luck,

Regards,
Erwin Moller
 
Reply With Quote
 
 
 
 
Frederik Vanderstraeten
Guest
Posts: n/a
 
      02-08-2007
Erwin Moller schreef:
> I never had a real-life situation where I couldn't encode an Array in the
> simple way I just described, BUT: If your array is really complex, and
> contains arrays itself, you better start looking at other solutions, maybe
> JSON (I never used that, so maybe I talk nonsense).


JSON would work perfectly. It has libraries in almost all modern
languages allowing you to transparently change a Javascript array into
an array in any other programming language.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Javascript array to servlet Garg Java 2 02-10-2007 05:57 AM
Passing Servlet Context to a non servlet object. Andrew Purser Java 3 11-04-2004 02:54 AM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
Tomcat: POST from one servlet or internal class to another servlet Sean Clarke Java 1 01-07-2004 02:22 PM
how to get my servlet configuration before the servlet is initialised Andy Fish Java 4 12-17-2003 09:47 PM



Advertisments