21

Ello there,

I'm trying to assign the value of a javascript variable to a java variable. But I don't have clue how to do this? Say for example I have this:

<html>
<head>
   <script type="text/javascript">
       function return variable(){
          var a = "hello";
          return a;
       }
   </script>
</head>
<body>

<%
   //The java code
   String b = //how do I get that javascript variable here?
%>

</body>
</html>
stevevls
  • 10,675
  • 1
  • 45
  • 50
  • 4
    If you understand how jsp works, you'll know that the java part executes on the server side `before` the page gets rendered in browser. This means it has already executed by the time any javascript gets any chance to execute. So you need to rethink what you are trying to do here. – d-live Nov 25 '11 at 11:26
  • Possible duplicate: http://stackoverflow.com/questions/1944330/assigning-values-from-javascript-to-java-variable – Alex K Nov 25 '11 at 12:55
  • possible duplicate of [How do I pass JavaScript values to Scriptlet in JSP?](http://stackoverflow.com/questions/5701031/how-do-i-pass-javascript-values-to-scriptlet-in-jsp) – cHao Nov 26 '11 at 04:00
  • Well actually http://stackoverflow.com/questions/11672500/can-i-build-a-rhino-javaadapter-in-java-using-a-scriptableobject – user2754112 Sep 28 '13 at 07:48
  • Old question but ppl who are saying simply "You cant" are all wrong, Yes you can and it depends on your environment, maybe iam using jsp, jsp or something else , why do you say "you cant"? Interesting.. –  Mar 24 '17 at 14:26

7 Answers7

21

Java script plays on browser where java code is server side thing so you can't simply do this.

What you can do is submit the calculated variable from javascript to server by form-submission, or using URL parameter or using AJAX calls and then you can make it available on server

HTML

<input type="hidden" id="hiddenField"/>

make sure this fields lays under <form>

Javascript

document.getElementById("hiddenField").value=yourCalculatedVariable;

on server you would get this as a part of request

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Hmm I see.. Because all in all what I'm trying to do. Is have a HTML form in the JSP, and with an onClick event it triggers the javascript method that then returns a variable. Then that variable can just get sent through with the rest of the form input values... So deff no way to do this? –  Nov 25 '11 at 11:26
  • You can set it in some hidden fields and then you can send it over the form submision. – jmj Nov 25 '11 at 11:30
  • Something like this: ? –  Nov 25 '11 at 11:31
1

You need to read something about a JSP's lifecycle. Try this: http://en.wikipedia.org/wiki/File:JSPLife.png

JavaScript runs on the client, but in order to change the jsp, you need access to the server. This can be done through Ajax(http://en.wikipedia.org/wiki/Ajax_%28programming%29).

Here are some Ajax-related links: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first

Dragos
  • 2,911
  • 12
  • 39
  • 55
1

The answer is You can't. Java (in your case JSP) is a server-side scripting language, which means that it is compiled and executed before all javascript code. You can assign javascript variables to JSP variables but not the other way around. If possible, you can have the variable appear in a QueryString or pass it via a form (through a hidden field), post it and extract the variable through JSP that way. But this would require resubmitting the page.

Hope this helps.

talha2k
  • 24,937
  • 4
  • 62
  • 81
1

JavaScript is fired on client side and JSP is on server-side. So I can say that it is impossible.

Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59
Jwalin Shah
  • 2,451
  • 1
  • 17
  • 22
0

I think there's no way to do that, unless you pass the value of the JavaScript var on the URL, but it's a ugly workaround.

fonini
  • 3,243
  • 5
  • 30
  • 53
0

you cant do it.. because jsp is compiled and converted into html server side whereas javascript is executed on client side. you may set the value to a hidden html element and send to servlet in request just in case you want to use for further

dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
  • Ah that is perfect, I can do that, it still solves my problem that way. So I'll just set the value in the hidden input type like this: ? –  Nov 25 '11 at 11:33
  • yeah you are right,you have to set the value of this element in the method itself and keep that element inside form so that it will added in url when the form is submitted. – dku.rajkumar Nov 25 '11 at 11:36
0

As JavaScript is client side and JSP is Server side.

So Javascript does not execute until it gets to the browser, But Java executes on the server. So, Java does not know the value of the JavaScript variable.

However you assign value of Java variable to JavaScript variable.

gprathour
  • 14,813
  • 5
  • 66
  • 90