473,322 Members | 1,307 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,322 software developers and data experts.

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 getCheckedValues(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=getCheckedValues(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.getParameterValues("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=" + getCheckedValues(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 11030
Oleg Konovalov wrote:

| Newsgroups:
| comp.lang.javascript,comp.lang.java.programmer,com p.lang.java.help,
| comp.lang.java.softwaretools,comp.lang.java

You must be kidding. Stripped down to comp.lang.javascript
and comp.lang.java.programmer, Followup-To comp.lang.javascript.
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 getCheckedValues(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=getCheckedValues(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 getCheckedValues() 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.getParameterValues("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=" + getCheckedValues(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*********@web.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.getParameterValues("v2");
and later...
<input type="hidden" name="v2"/>
in "onclick" event of page1 I execute JavaScript"

if (this.v1) v2.value=getCheckedValues(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 getCheckedValues returns. Although it would have been nice
if you provided the code to getCheckedValues, there is one thing I can
definitely tell you. You don't want to be calling getParameterValues in
the servlet.

The getParameterValues 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=first&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=first,second,third

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

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.getParameter("v2");
String[] ids = paramValue.split(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=first,second,third Yes, if I call getParameterValues(v2) with
window.location.href="page2?v2=" + getCheckedValues(v1);
in the URL I see: ...page2?v2=123,456,789...&<other params>
The problem is that I can't get that value "123,456,789.." 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.net> 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.getParameterValues("v2");
and later...
> <input type="hidden" name="v2"/>
> in "onclick" event of page1 I execute JavaScript"
>
> if (this.v1) v2.value=getCheckedValues(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 getCheckedValues returns. Although it would have been nice
if you provided the code to getCheckedValues, there is one thing I can
definitely tell you. You don't want to be calling getParameterValues in
the servlet.

The getParameterValues 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=first&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=first,second,third

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

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.getParameter("v2");
String[] ids = paramValue.split(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.infosystems.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 interoperability of your
documents if you followed the alternatives.
[top post]


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

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

Oleg Konovalov <ok******@verizon.net> wrote:
Yes, if I call getParameterValues(v2) with
window.location.href="page2?v2=" + getCheckedValues(v1);
in the URL I see: ...page2?v2=123,456,789...&<other params>
The problem is that I can't get that value "123,456,789.." 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******@verizon.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:attribute name='value'><xsl: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******@verizon.net> wrote:
Yes, if I call getParameterValues(v2) with
window.location.href="page2?v2=" + getCheckedValues(v1);
in the URL I see: ...page2?v2=123,456,789...&<other params>
The problem is that I can't get that value "123,456,789.." 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
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) {...
10
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...
3
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... ...
1
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,...
1
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...
41
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...
2
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...
1
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.