473,836 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing Javascript array to Java

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).

So I created a Javascript function:
function getCheckedValue s(checkbox),
which returns an array of strings (value's of checked checkboxes).
On page1 I assign it to the hidden field.

<input type="hidden" name="v2"/>
in "onclick" event of page1 I execute JavaScript"

if (this.v1) v2.value=getChe ckedValues(v1);
window.location .href="page2?v2 =" + v2.value;

On page2 I also have:
<input type="hidden" name="v2"/>
In the Java class attached to page2 I try to do:
String [] ids = request.getPara meterValues("v2 ");
It seems to create an array with 1 member which is blank.

I am getting the same result if I try to pass a
window.location .href="page2?v2 =" + getCheckedValue s(v1)
but at least I can see some comma separated id's in the IE URL.

Why is it not working ?
Is there a better solution ?

Please help !
Thank you in advance,
Oleg.

P.S.: I tried to google around and found this page:
http://www.irt.org/script/1433.htm
But I don't understand the second part of the code (looks like an incomplete
hack to me)
and need to receive that array from Java, not a Javascript.
Dec 1 '05 #1
7 11096
Oleg Konovalov wrote:

| Newsgroups:
| comp.lang.javas cript,comp.lang .java.programme r,comp.lang.jav a.help,
| comp.lang.java. softwaretools,c omp.lang.java

You must be kidding. Stripped down to comp.lang.javas cript
and comp.lang.java. programmer, Followup-To comp.lang.javas cript.
On page1 I have a bunch of checkboxes with the same name v1
but different values (= DB rowId).

So I created a Javascript function:
function getCheckedValue s(checkbox),
which returns an array of strings (value's of checked checkboxes).
On page1 I assign it to the hidden field.

<input type="hidden" name="v2"/>
in "onclick" event of page1 I execute JavaScript"

if (this.v1) v2.value=getChe ckedValues(v1);
window.location .href="page2?v2 =" + v2.value;
I do not think this is necessary or desirable. Use a `form' element
(with `action' attribute omitted or set to "GET") instead, and let
the UA compose the query-part.

The value of `this.v1', the body of the getCheckedValue s() method
and the value of `v1' on call are missing for further analysis.
On page2 I also have:
<input type="hidden" name="v2"/>
In the Java class attached to page2
You have to elaborate. Either it is a markup document or it is executable
code. It cannot be both (it can be with different contexts). So far the
above is only an input[type="hidden"] element without value. How is it
connected to the Java class?
I try to do:
String [] ids = request.getPara meterValues("v2 ");
Where? Especially: client-side or server-side Java? Which runtime
environment, which version (UA/JRE)?
It seems to create an array with 1 member which is blank.

I am getting the same result if I try to pass a
window.location .href="page2?v2 =" + getCheckedValue s(v1)
but at least I can see some comma separated id's in the IE URL.
Useless information. What is needed for analysis is real test results, not
some loose description of it.
Why is it not working ?
Impossible to tell, not enough input provided. For example, it would
help if you posted the HTTP request (at least the GET header) as-is.
Is there a better solution ?
Certainly there is.
P.S.: I tried to google around and found this page:
http://www.irt.org/script/1433.htm
But I don't understand the second part of the code (looks like an
incomplete hack to me)
and need to receive that array from Java, not a Javascript.


If you mean by that you want to access the data provided by the UA from
Java code, ask in a Java group, not here.

If you mean by that you want to access the data provided by Java code in
the UA (through DOM host objects), you have to elaborate on that code.
PointedEars
Dec 1 '05 #2
Restoring comp.lang.java. programmer and setting followups there.

Thomas 'PointedEars' Lahn <Po*********@we b.de> wrote:

Where? Especially: client-side or server-side Java? Which runtime
environment, which version (UA/JRE)?
The Java code that Oleg posted was written to the servlet API, of any
version. Therefore, it's server-side. Its behavior doesn't depend on
Java or Servlet API versions. So he lucked out here.
If you mean by that you want to access the data provided by the UA from
Java code, ask in a Java group, not here.
Turns out that he made the opposite mistake; posting to too many Java
groups (and one non-Java group in the process).

My answer is below.
Oleg Konovalov wrote:
In the Java class attached to page2 I try to do:
String [] ids = request.getPara meterValues("v2 ");
and later...
<input type="hidden" name="v2"/>
in "onclick" event of page1 I execute JavaScript"

if (this.v1) v2.value=getChe ckedValues(v1);
window.location .href="page2?v2 =" + v2.value;


You're submitting only ONE form parameter, with a name of v2 and a value
of whatever getCheckedValue s returns. Although it would have been nice
if you provided the code to getCheckedValue s, there is one thing I can
definitely tell you. You don't want to be calling getParameterVal ues in
the servlet.

The getParameterVal ues method is intended to be used where there is
potentially more than one value for the same name. That is, if (using
HTTP GET) your URL looked something like:

...page2?v2=fir st&v2=second&v2 =third

That isn't what you're doing, though. Based on what you wrote, it
sounds like you're doing this instead:

...page2?v2=fir st,second,third

That is providing only ONE parameter, with a name of "v2" and a value of
"first,second,t hird". If you call getParameterVal ues, it will work
fine, but it will return an array of length one, and the only element
will be the String "first,second,t hird".

If you want to keep that URL format, you need to modify your servlet
code to parse the parameter. Something like this:

String paramValue = request.getPara meter("v2");
String[] ids = paramValue.spli t(Pattern.quote (","));

(If using a Java version prior to 1.5, Pattern.quote won't be available.
In that case, replace Pattern.quote(" ,") with "\\," comma. Even the
backslash there is not strictly necessary, but that is good form since
commas are reserved Java regexp syntax in at least some situations. If
your Java version is prior to 1.4, though, then you'll need to use
StringTokenizer instead of the newer String.split in the first place.)

But you really ought to also take Thomas's advice, and use a proper HTML
form instead of your JavaScript-with-URL-building hack. This would have
saved you a lot of confusion, since there are plenty of tutorials on how
to read HTML form content from servlets.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Dec 1 '05 #3
Chris/Thomas,

Yes, it is a server-side java, servlet type (actually, it's a Cocoon 2.0.4
application with sitemap actions,
but I didn't want to scare anybody off), JRE 1.4.2.
...page2?v2=fir st,second,third Yes, if I call getParameterVal ues(v2) with
window.location .href="page2?v2 =" + getCheckedValue s(v1);
in the URL I see: ...page2?v2=123 ,456,789...&<ot her params>
The problem is that I can't get that value "123,456,78 9.." in Java (get one
empty string).
Do you know how to do that ?

I thought that Javascript array disappears after page1 is replaced by page2
in the browser.
That's why I try to create a hidden input v2 on page1 to store the array of
checked checkbox values.
Is that necessary ?

I have no problems parsing that string and creating String array out of it.

But you really ought to also take Thomas's advice, and use a proper HTML
form instead of your JavaScript-with-URL-building hack. This would have
saved you a lot of confusion, since there are plenty of tutorials on how
to read HTML form content from servlets.

Please elaborate on "use a proper HTML form".
As I mentioned, I am trying to pass that Javascript array (or set of
checkboxes)
from one page (form) to another.
If there a better way of doing it, please give more details.

I do not think this is necessary or desirable. Use a `form' element
(with `action' attribute omitted or set to "GET") instead, and let
the UA compose the query-part. What does "let the UA compose the query part" mean ? Please elaborate.
Both web pages are in XSLT and there are other reasons why I call page2 via
URL "onclick" event.
Do not want to put a lot of less relevant info here.
Thank you in advance,
Oleg.

"Chris Smith" <cd*****@twu.ne t> wrote in message
news:MP******** *************** *@news.altopia. net...
Oleg Konovalov wrote:
> In the Java class attached to page2

> I try to do:
> String [] ids = request.getPara meterValues("v2 ");
and later...
> <input type="hidden" name="v2"/>
> in "onclick" event of page1 I execute JavaScript"
>
> if (this.v1) v2.value=getChe ckedValues(v1);
> window.location .href="page2?v2 =" + v2.value;


You're submitting only ONE form parameter, with a name of v2 and a value
of whatever getCheckedValue s returns. Although it would have been nice
if you provided the code to getCheckedValue s, there is one thing I can
definitely tell you. You don't want to be calling getParameterVal ues in
the servlet.

The getParameterVal ues method is intended to be used where there is
potentially more than one value for the same name. That is, if (using
HTTP GET) your URL looked something like:

...page2?v2=fir st&v2=second&v2 =third

That isn't what you're doing, though. Based on what you wrote, it
sounds like you're doing this instead:

...page2?v2=fir st,second,third

That is providing only ONE parameter, with a name of "v2" and a value of
"first,second,t hird". If you call getParameterVal ues, it will work
fine, but it will return an array of length one, and the only element
will be the String "first,second,t hird".

If you want to keep that URL format, you need to modify your servlet
code to parse the parameter. Something like this:

String paramValue = request.getPara meter("v2");
String[] ids = paramValue.spli t(Pattern.quote (","));

(If using a Java version prior to 1.5, Pattern.quote won't be available.
In that case, replace Pattern.quote(" ,") with "\\," comma. Even the
backslash there is not strictly necessary, but that is good form since
commas are reserved Java regexp syntax in at least some situations. If
your Java version is prior to 1.4, though, then you'll need to use
StringTokenizer instead of the newer String.split in the first place.)

But you really ought to also take Thomas's advice, and use a proper HTML
form instead of your JavaScript-with-URL-building hack. This would have
saved you a lot of confusion, since there are plenty of tutorials on how
to read HTML form content from servlets.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Dec 1 '05 #4
Oleg Konovalov wrote:
Chris/Thomas,
In a one-to-many medium like Usenet, it does not make much sense to address
people specifically via name (although it can be useful at times ;-)). It
is much more important that quoted material is being provided in a usable
fashion along with proper attribution:

<URL:http://jibbering.com/faq/faq_notes/pots1.html>
Yes, it is a server-side java, servlet type (actually, it's a Cocoon 2.0.4
application with sitemap actions,
but I didn't want to scare anybody off), JRE 1.4.2.
You did not want to scare anybody off, but posted to almost the whole world?
OK, I am exaggerating, but I hope you see the point.
[...]
I thought that Javascript array disappears after page1 is replaced by
page2 in the browser.
True, however if you submitted the information from one document resource
to another or stored it in session variables or cookies, you would not need
to preserve the array.
That's why I try to create a hidden input v2 on page1 to store the array
of checked checkbox values.
Is that necessary ?
No, the checkbox values are automatically transferred if the respective form
controls have a `name' attribute.
But you really ought to also take Thomas's advice, and use a proper HTML
form instead of your JavaScript-with-URL-building hack. This would have
saved you a lot of confusion, since there are plenty of tutorials on how
to read HTML form content from servlets.


Please elaborate on "use a proper HTML form".
As I mentioned, I am trying to pass that Javascript array (or set of
checkboxes)
from one page (form) to another.
If there a better way of doing it, please give more details.


<form action="foobar" >
<input type="checkbox" name="foo" checked>
<input type="submit" ...>
</form>

Clicking the submit button results in

GET .../foobar?foo=1 HTTP/1.x

If the checkbox is not checked, it is either `foo=0' or there is no
`foo' name-value pair in the query-part.

You should have asked in comp.infosystem s.www.authoring.html (ciwah).
I do not think this is necessary or desirable. Use a `form' element
(with `action' attribute omitted or set to "GET") instead, and let
the UA compose the query-part.

What does "let the UA compose the query part" mean ? Please elaborate.


See above.
Both web pages are in XSLT
I do not think so. XSLT is the XSL Transformation Language. It transforms
markup into different markup according to an XSL stylesheet. The latter
markup, probably (X)HTML, is the markup client-side scripts must operate
on. Use "View Source", Luke.
and there are other reasons why I call page2 via URL "onclick" event.
Do not want to put a lot of less relevant info here.
Using XSLT does not strike me as a valid reason for insisting on
client-side scripting where the alternative is not only more
reliable but, as here, readily available.

Name the other reasons, maybe it can be shown that they are not
substantial as well which would increase interoperabilit y of your
documents if you followed the alternatives.
[top post]


See above.
F'up2 comp.lang.javas cript, please follow!

PointedEars
Dec 1 '05 #5
F'up re-set.

Oleg Konovalov <ok******@veriz on.net> wrote:
Yes, if I call getParameterVal ues(v2) with
window.location .href="page2?v2 =" + getCheckedValue s(v1);
in the URL I see: ...page2?v2=123 ,456,789...&<ot her params>
The problem is that I can't get that value "123,456,78 9.." in Java (get one
empty string).
Do you know how to do that ?


I already told you how to retrieve the value.

If it's not working, then there are only two possibilities: (1) you are
working with a broken servlet container, or (2) there is a servlet
filter mucking with your parameters.

Far more probable, though, is that you think it's not working when in
truth you're doing something wrong. You still haven't posted enough
information to figure out what could really be going wrong. Can you
please post a complete example that reproduces the problem? Hint: ditch
the XSLT and JavaScript for the time being, and just type in the URL.
We're going for the least possible number of lines of code that will
cause the problem to occur, and we'd like to see all of those lines of
code. Then post the ENTIRE URL that you use to access the servlet,
starting with http:// and all the way to the end. Please copy and paste
to avoid typos.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Dec 1 '05 #6

"Oleg Konovalov" <ok******@veriz on.net> wrote in message
news:Q7rjf.348$ GA2.70@trndny02 ...
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)


When you say "Java on a web page", do you mean JSP or Applets?

- Oliver
Dec 1 '05 #7
Guys,

I solved the problem.
There was parameter assignment missing on XSL side of things,
from the URL to the page2 (I was doing it before, but the guy from
XSL newsgroup talked me out of it ;-( ).

<input type="hidden" name="v2">
<xsl:attribut e name='value'><x sl:value-of
select='$v'/></xsl:attribute>
</input>

I was doing POST, parameters in URL were needed for the input into XSL,
not page request parameters.
Thank you for your help, everybody !

Oleg.
Chris Smith wrote:
F'up re-set.

Oleg Konovalov <ok******@veriz on.net> wrote:
Yes, if I call getParameterVal ues(v2) with
window.location .href="page2?v2 =" + getCheckedValue s(v1);
in the URL I see: ...page2?v2=123 ,456,789...&<ot her params>
The problem is that I can't get that value "123,456,78 9.." in Java (get one
empty string).
Do you know how to do that ?


I already told you how to retrieve the value.

If it's not working, then there are only two possibilities: (1) you are
working with a broken servlet container, or (2) there is a servlet
filter mucking with your parameters.

Far more probable, though, is that you think it's not working when in
truth you're doing something wrong. You still haven't posted enough
information to figure out what could really be going wrong. Can you
please post a complete example that reproduces the problem? Hint: ditch
the XSLT and JavaScript for the time being, and just type in the URL.
We're going for the least possible number of lines of code that will
cause the problem to occur, and we'd like to see all of those lines of
code. Then post the ENTIRE URL that you use to access the servlet,
starting with http:// and all the way to the end. Please copy and paste
to avoid typos.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Dec 1 '05 #8

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

Similar topics

3
4346
by: Raju V.K | last post by:
can I use javascript and PHP in the following manner to create a pop-up window: <--- in <head> </head> <script language=javascript> function popup(folder1, file1) { window1=open("solution.php?folder=folder1&file=file1"); }
10
2041
by: John Ortt | last post by:
Hi Everyone, I have created a Javascript menu for my site which uses frames. The first stage loads fine but I want two drill down menus ("About Me Menu" and "Projects Menu"). The pages load fine, but the images aren't displayed, instead the alt text is shown. The images come up after you "Mouse Over" them but not before......can anybody spot any errors in my code which could be doing this please. For info, I have 4 images for each...
3
2173
by: John Ortt | last post by:
I appologise for reposting this but I have been trying to find a solution all week with no avail and I was hoping a repost might help somebody more knowledgable than myself to spot the message... I am trying to write a webpage in Javascript. The page loads with three frames, a top, left and right frame. Depending on the users actions the background page rewrites the code for the relevant frames.
1
8512
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).
1
2506
by: Eric Capps | last post by:
This may be more of a Java question, but I feel that JavaScript experts may be more qualified to help me find a solution. In short: is it possible to call a Java method from JavaScript, passing as an argument a JavaScript array (in my case, an array of doubles)? Supposing I have array x containing only doubles, I have tried: document.MyApplet.myMethod(x); But the method only ever seems to receive "null" as an argument.
41
4995
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in the hash are alphabetically sorted if the key happens to be alpha numeric. Which I believe makes sense because it allows for fast lookup of a key.
2
12407
by: Garg | last post by:
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.
1
1497
by: mariaz | last post by:
Hello, I have an array in my javascript file: var locations=new Array(); and I want to pass the attributes of the array into my java file, does anyone know how can this be done? Thank you in advance!
0
9658
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
10533
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10579
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
6975
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5644
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4443
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
4003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3103
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.