assign JavaScript variable to Java variable problem in JSP

Discussion in 'Java' 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

    Hal Rosser Guest

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

    Matt,
    Javascript does not execute until it gets to the browser,
    Java executes on the server as you know.
    Java does not know the value of the JavaScript variable.
    HTH
    Hal


    ---
    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/24/2004
    Hal Rosser, Sep 26, 2004
    #2
    1. Advertising

  3. (Matt) writes:

    > <% b %> = x;


    In addition to the response you got, "b" is not a legal Java
    statement, which gives the compiler error that the 500 error probably
    wraps.
    Tor Iver Wilhelmsen, Sep 26, 2004
    #3
  4. Matt

    Tim Jowers Guest

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


    Maybe post this question in a MSFT group. server side javascript is
    their thing if I remmebr correctly.
    Tim Jowers, Sep 27, 2004
    #4
  5. Matt

    Tim Jowers Guest

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


    Maybe post this question in a MSFT group. server side javascript is
    their thing if I remmebr correctly.
    Tim Jowers, Sep 27, 2004
    #5
  6. Matt

    Bryce Guest

    On 24 Sep 2004 22:52:24 -0700, (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;


    That's because of this fundamental difference between JSP and
    JavaScript:

    JavaScript: run on the client's browser.
    JSP: Compiled into a Servlet, and runs on the server, before
    responding to a request.

    Therefore, JSP can write to the JavaScript variable because its part
    of the page that's to be displayed. JSP is evaluated BEFORE the page
    is displayed on your browser.

    Try this experiment:

    take your example #1 abouve, and view source. You'll see

    var x = 10

    in the source. Notice you do not see:

    int a = 10;

    anywhere.

    --
    now with more cowbell
    Bryce, Sep 27, 2004
    #6
  7. Matt

    Bryce Guest

    On 27 Sep 2004 10:26:49 -0700, (Tim Jowers) wrote:

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

    >
    >Maybe post this question in a MSFT group. server side javascript is
    >their thing if I remmebr correctly.


    Wasn't there a Netscape web server/servlet container that had server
    side javascript? I seem to remember someone here around the office
    complaining about having to support a customer that was using that...

    Ahh yes, iPlanet...

    --
    now with more cowbell
    Bryce, Sep 27, 2004
    #7
  8. On Mon, 27 Sep 2004 15:50:11 -0400, Bryce wrote:

    >>Maybe post this question in a MSFT group. server side javascript is
    >>their thing if I remmebr correctly.

    >
    > Wasn't there a Netscape web server/servlet container that had server
    > side javascript? I seem to remember someone here around the office
    > complaining about having to support a customer that was using that...
    >
    > Ahh yes, iPlanet...


    I suspect this mention of server-side JavaScript is confusing
    the OP, if anything.

    a) It is very rare, (as alluded to above, though I think there
    are other forms of SS JS)

    b) He has (and apparently needs) calculation ability on
    both the client and server.

    c) He already has server-side JSP, so use it.

    d) The client-side calculations can be done in JS, which
    then does a get/post back to the server to transmit information
    for consideration/calculation by JSP's on the server.

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.lensescapes.com/ Images that escape the mundane
    Andrew Thompson, Sep 28, 2004
    #8
  9. Matt

    saravanavsk

    Joined:
    Jan 25, 2010
    Messages:
    2
    i got the point that we can't assign javascript variables to java variable in jsp. Then in the situation ,,,,,i am having the various textbox going to display the employee details ,,,so after getting the employee id,,and click the view button , i like to display the employee details on the corresponding textbox in the same jsp page.
    How can i do this , without having the id value from the text box for the database connection. /*select * from emp where emp id=?????*/
    saravanavsk, Jan 25, 2010
    #9
    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:
    0
    Views:
    4,358
  2. Ranjan
    Replies:
    1
    Views:
    2,760
    Eliyahu Goldin
    Apr 10, 2007
  3. Matt
    Replies:
    2
    Views:
    855
    Anthony Judd
    Sep 25, 2004
  4. Replies:
    4
    Views:
    1,916
    Anthony Jones
    Oct 9, 2006
  5. Matt
    Replies:
    2
    Views:
    241
    Hal Rosser
    Sep 26, 2004
Loading...

Share This Page