assign JavaScript variable to Java variable problem in JSP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt

    assign JavaScript variable to Java variable problem in JSP

    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!
  • Richard Cornford

    #2
    Re: assign JavaScript variable to Java variable problem in JSP

    Matt wrote:[color=blue]
    > 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;[/color]

    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.


    Comment

    • Hal Rosser

      #3
      Re: assign JavaScript variable to Java variable problem in JSP

      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" <jrefactors@hot mail.com> wrote in message
      news:ba8a039e.0 409242153.4158f 9f4@posting.goo gle.com...[color=blue]
      > 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![/color]


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


      Comment

      Working...