assign JavaScript variable to Java variable problem in JSP

Discussion in 'Javascript' started by Matt, Sep 25, 2004.

  1. Matt

    Matt Guest

    If I assign Java variable a to javascript variable x, it is
    fine.
    <%
    int a = 10;
    %>
    var x = <%= a %>;
    alert(x);

    But if I do the other way around, then it has 500 error. any ideas??

    <%
    int b;
    %>
    <% b %> = x;


    thanks!
     
    Matt, Sep 25, 2004
    #1
    1. Advertising

  2. Matt wrote:
    > If I assign Java variable a to javascript variable x, it is
    > fine.
    > <%
    > int a = 10;
    > %>
    > var x = <%= a %>;
    > alert(x);
    >
    > But if I do the other way around, then it has 500 error.
    > any ideas??
    >
    > <%
    > int b;
    > %>
    > <% b %> = x;


    With server-side scripting one of the most fundamental things that needs
    to be grasped is what runs where and when. The approximate process
    goes:-

    1. Browser sends a request to a server.
    2. Server discovers that the request is for a JSP
    and *executes* the method of that JSP that builds
    a response (usually HTML page source code that may
    include client-side javascript).
    3. The response is sent back to the browser.
    4. The browser receives the response and displays the
    result, executing any javascript that may be included.

    As a result the JSP should be considered to have finished before the
    javascript has even started. The JSP, executing first, may write values
    into any javascript source code on the page but the only way a
    client-side javascript variable value is going to get anywhere near a
    JSP is if it is sent back as part of a query string on a GET request or
    as part of a POST request.

    Richard.
     
    Richard Cornford, Sep 25, 2004
    #2
    1. Advertising

  3. Matt

    Hal Rosser Guest

    Since Java runs on the server, and javascript waits until it gets to the
    browser, Java does not know what the value of the javascript variable is. It
    don't work now, and it won't work - unless you include a var's value in the
    querystring you send to the jsp.

    "Matt" <> wrote in message
    news:...
    > If I assign Java variable a to javascript variable x, it is
    > fine.
    > <%
    > int a = 10;
    > %>
    > var x = <%= a %>;
    > alert(x);
    >
    > But if I do the other way around, then it has 500 error. any ideas??
    >
    > <%
    > int b;
    > %>
    > <% b %> = x;
    >
    >
    > thanks!



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.769 / Virus Database: 516 - Release Date: 9/25/2004
     
    Hal Rosser, Sep 26, 2004
    #3
    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. Matt
    Replies:
    8
    Views:
    53,786
    saravanavsk
    Jan 25, 2010
  2. Replies:
    0
    Views:
    4,441
  3. Ranjan
    Replies:
    1
    Views:
    2,792
    Eliyahu Goldin
    Apr 10, 2007
  4. Matt
    Replies:
    2
    Views:
    896
    Anthony Judd
    Sep 25, 2004
  5. Replies:
    4
    Views:
    1,991
    Anthony Jones
    Oct 9, 2006
Loading...

Share This Page