0

I have a variable declared outside my javascript function which gets its value from session.

   String elementID = session.getAttribute("elementID");
   <SCRIPT>
        var a = "123"
        elementID = a;
   </SCRIPT>

Will this work out? i need to assign the value "123" to the string variable elementID.

lal1990
  • 11
  • 1
  • 4
  • 9
    JSP is server side. Javascript is client side. **That's the first thing you need to understand.** – Sotirios Delimanolis Mar 05 '14 at 18:02
  • In other words, **no** it will not work. – Pointy Mar 05 '14 at 18:03
  • It is will just fine if you serialize the value. But you definitely need to understand the difference between those two languages and why that wouldn't fundamentally work. – Matthew Cox Mar 05 '14 at 18:06
  • It's quite unfortunate that JavaScript has "Java" in its name. You wouldn't expect C++ to share its variables with JavaScript, would you? – MMM Mar 05 '14 at 18:07
  • If you are generating the Javascript as part of a page that is being generated by Java (such as in a JSP), then you can certainly embed values from Java into the Javascript -- at that point, you are merely assembling some text which is going to be sent down to the browser -- but this is not the right syntax for that. – David Conrad Mar 05 '14 at 18:17

1 Answers1

0

This is not simply possible, since Java runs server-side and JavaScript client-side.

You can't assume that the JavaScript (that won't be evaluated until it's on the client) knowns anything about Java. It doesn't. 

Max
  • 12,622
  • 16
  • 73
  • 101
  • Actually, [it is possible to invoke Java functions from JavaScript in the browser](http://stackoverflow.com/questions/11536455/calling-java-method-in-javascript). Of course, the JavaScript and Java code will need to be written in separate files. – Anderson Green Mar 05 '14 at 18:06