473,839 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getPara meterValues 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 12407
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.getPara meterValues 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,89 7,1254);
to a string:
"1,4,67,897,125 4"
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
4274
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 args) throws IOException { URL url = new
2
2476
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 web xml <servet-mapping> is :
7
11098
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, need to iterate through pages page2 - e.g. allow user to update the fields there) On page1 I have a bunch of checkboxes with the same name v1 but different values (= DB rowId).
10
2685
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 blah blah. With Ajax (actually, the uncovering of the XmlHTTPRequest object) I absolutely see the benefit of moving more of the UI work to the client, rather than doing page refreshes. I know there are a bunch of libraries out there...
5
28664
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 page) with parameters then parse the image content to receive the server response stored in the image.
1
7550
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 import='java.io.*,java.util.*,java.sql.*,javax.servlet.*,javax.servlet.http.*' session='true'%> <%@ page import='java.text.*'%> <html> <script language='javascript'> function backward() { alert("hello"); }
3
2184
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 giving me an unspecified error. I decided to test my theory and created a generic function to pass over my url string and sure enough javascript dies with the same error. I figured one way around it was to split my string into an
2
3461
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 advance for responding. I have made a stub of the codes for better understanding. Situation: I have a Java servlet (HelloWorldServlet) running on a Tomcat (5.5.26) server on a Linux machine (compiled in JDK1.4.2). It writes Javascript to construct...
7
8887
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 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...
0
9697
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10908
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10649
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10295
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4487
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.