Passing Javascript array to Java

Discussion in 'Java' started by Oleg Konovalov, Dec 1, 2005.

  1. 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.
     
    Oleg Konovalov, Dec 1, 2005
    #1
    1. Advertising

  2. Oleg Konovalov wrote:

    | Newsgroups:
    | comp.lang.javascript,comp.lang.java.programmer,comp.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
     
    Thomas 'PointedEars' Lahn, Dec 1, 2005
    #2
    1. Advertising

  3. Oleg Konovalov

    Chris Smith Guest

    Restoring comp.lang.java.programmer and setting followups there.

    Thomas 'PointedEars' Lahn <> 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
     
    Chris Smith, Dec 1, 2005
    #3
  4. 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" <> wrote in message
    news:...

    >> 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
     
    Oleg Konovalov, Dec 1, 2005
    #4
  5. 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
     
    Thomas 'PointedEars' Lahn, Dec 1, 2005
    #5
  6. Oleg Konovalov

    Chris Smith Guest

    F'up re-set.

    Oleg Konovalov <> 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
     
    Chris Smith, Dec 1, 2005
    #6
  7. Oleg Konovalov

    Oliver Wong Guest

    "Oleg Konovalov" <> 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
     
    Oliver Wong, Dec 1, 2005
    #7
  8. Oleg Konovalov

    Guest

    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 <> 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, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    581
    justanotherguy
    Dec 3, 2004
  2. Jonathan Underwood
    Replies:
    2
    Views:
    662
    Simon Biber
    Aug 13, 2003
  3. JW
    Replies:
    1
    Views:
    462
    David Jeske
    Jul 12, 2003
  4. deadpickle
    Replies:
    1
    Views:
    1,032
    Jens Thoms Toerring
    Nov 7, 2010
  5. Oleg Konovalov

    Passing Javascript array to Java

    Oleg Konovalov, Dec 1, 2005, in forum: Javascript
    Replies:
    5
    Views:
    239
Loading...

Share This Page