473,395 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Passing Javascript array to servlet

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

Feb 8 '07 #1
2 12373
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
Feb 8 '07 #2
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.
Feb 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Patrick | last post by:
I am trying to download the html page at http://www.dreamteamfc.com/dtfc04/servlet/PostPlayerList?catidx=1 Using the code public class DownloadWebPage { public static void main (String...
2
by: Rose Girl | last post by:
Hi Need some help on javascript. I need to know how to call a servlet from a javascript function. web-inf (jboss) path for servlet is: com.purpleace.ripple.admin.contacts.servlet.AdminReport...
7
by: Oleg Konovalov | last post by:
Hi, I am trying to pass a bunch of checked checkboxes (Javascript array) from page1 to the Java action class on subsequent web page (page2). (on page 1 I have a bunch of DB rows with a checkbox,...
10
by: Tom Cole | last post by:
While I've done javascript work for as long as I can remember (since Netscape first released it), I've only ever used it for trivial things, change a color here, validate a text element there, blah...
5
by: thierryParent66 | last post by:
Hi. I am trying to load image from another domain and reading content. How an I get the Image array of bytes after loading it. What I want is to call a server (on another domain as the current...
1
by: anupamaavadutha | last post by:
hi all, iam new to javascript. i have problem calling javascript functions.iam designing a calender page.here is my code. <%@ page...
3
by: jerm | last post by:
Hi, I am using ajax to update a form. I call xmlHttp.open("GET", strURL, false). The problem is that strURL is a really long string and it seems that javascript craps out on this long string...
2
by: ManidipSengupta | last post by:
Hi, a few (3) questions for the Java experts, and let me know if this is the right forum. It deals with 100% java code (reason for posting here) but manages a Web browser with Javascript. Thanks in...
7
by: vinodsk101 | last post by:
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.