SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting a jsp var to that of a JS var

    Subject pretty much says it all.

    All I want to know is how I would set a Javascript variable to a JSP var.

    I can't figure it out for the life of me...

    Also, I need the JS var to be an Int, as I'm sending this to a java class that takes in an int as an argument.
    Last edited by Defender1; Oct 15, 2003 at 12:09.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just pass it in the query string, then Integer.parseInt() the value.
    ck :: bringing chris to the masses.

  3. #3
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Query string?

    I was doing parseInt, but I couldn't get the variable INTO JSP, that's my problem:
    Code:
    int intSsg = Integer.parseInt("<script>document.write(ssg)</script>" );
    That gives me this error:
    java.lang.NumberFormatException: For input string: ""
    Last edited by Defender1; Oct 15, 2003 at 12:03.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Defender1
    Subject pretty much says it all.

    All I want to know is how I would set a JSP variable to a Javascript var.

    I can't figure it out for the life of me...

    Also, I need the JS var to be an Int, as I'm sending this to a java class that takes in an int as an argument.
    Wouldn't this do it?
    Code:
    <%
     //declare the variable in JSP
     int foo = 4;
    %>
    <script type="text/javascript">
      var foo = parseInt(<% out.print(foo);%>);
    </script>

  5. #5
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bleh, i'm such a blind fool. I need to send a JS var TO jsp...
    Like I said earlier, I need to send a java class a JS variable.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  6. #6
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    He's trying to read a javascript variable from JSP, not vice versa. But you can't execute javascript code directly from JSP as far as I know.
    ck :: bringing chris to the masses.

  7. #7
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Anarchos
    He's trying to read a javascript variable from JSP, not vice versa. But you can't execute javascript code directly from JSP as far as I know.
    Exactly. JSP is server-side and Javascript is client-side. If you want JSP to read the JS variable you'd have to load it up in the querystring or load the value into a hidden form field, then reload the page or send the visitor to another page.

  8. #8
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am refreshing the page, but like i said, how can JSP get the var?
    Last edited by Defender1; Oct 16, 2003 at 05:18.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  9. #9
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No one else? [img]images/smilies/frown.gif[/img]

    And by querystring did ya mean the url (GET)?

    So basically that and a form are my only choices?
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  10. #10
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Defender1
    No one else? [img]images/smilies/frown.gif[/img]

    And by querystring did ya mean the url (GET)?

    So basically that and a form are my only choices?
    Pretty much, sorry.

  11. #11
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thanks. It sort of makes things easier though.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •