how to pass value from javascript to java in jsp

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

    how to pass value from javascript to java in jsp

    Hi

    in JSP, it's easy to pass value from java-variable to
    javascript-variable, like

    js_function(a)
    { a=<%java-class.A%>
    }

    I'm wondering how is the other way around? I tried

    js_function(a)
    { <%java-class.A=%>a
    }

    but there was compiling error.

    Can any one help?

    --
    Thanks
    John
    Toronto

  • Yereth

    #2
    Re: how to pass value from javascript to java in jsp

    You can only pass from JSP to JS in your code. If you want to pass
    variables the other way around then you'll have to use the POST or GET
    method, because JSP is serverside and JS clientside, which means that
    first, on the server, JSP is executed (it generates HTML and other
    clientside code, like PHP does) and second, that the generated source
    document is sent to the client where possible scripting languages like
    JS and VBS are executed.

    Hope that helps.

    Cheers,
    Yereth

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: how to pass value from javascript to java in jsp

      "john woo" <john_woo@canad a.com> writes:
      [color=blue]
      > in JSP, it's easy to pass value from java-variable to
      > javascript-variable, like
      >
      > js_function(a)
      > { a=<%java-class.A%>
      > }[/color]


      Notice what happens here: The java is evaluated and inserted
      in something that is just text. It happens to be a Javascript
      program, but the JSP template engine doesn't really care.
      To the server, where this runs, it's just text and Java.
      The output of the template is just text.
      [color=blue]
      > I'm wondering how is the other way around? I tried
      >
      > js_function(a)
      > { <%java-class.A=%>a
      > }[/color]

      The server doesn't care whether you write that or this:

      argleebargle <%java-class.A=%> glop glyf

      It's still just malformed Java and random text to it.


      The javascript is run on the client, i.e., in the browser. At that
      time, the Java is long gone, and only the output of the template
      remains. If you want to bring a value from the javascript, running
      on the client, to Java, running on the server, you need to send
      it to the server somehow - as part of an HTTP request.

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: how to pass value from javascript to java in jsp

        Yereth wrote:
        [color=blue]
        > You can only pass from JSP to JS in your code. If you want to pass
        > variables the other way around then you'll have to use the POST or
        > GET method, because JSP is serverside and JS clientside, [...][/color]

        The JS is probably client-side *here*.


        PointedEars
        --
        Bill Gates isn't the devil -- Satan made sure hell
        _worked_ before he opened it to the damned ...

        Comment

        Working...