Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to assign a javascript variable to java varisble?

 
gokulanand narayanan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

In my application i need to assign a javascript variable to java variable.

How can i achive this.

Thanks
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java variable value can assign into javascript variable but you can't use javascript variable inside java code.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a hidden field, set the value with JavaScript and when the form posts, read the value.

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java executes on the server. Javascript executes on the client.

Please read this article to see why your question doesn't make much sense.
 
Pat Flickner
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric's absolutely correct, but you can take it a step further. If you need it to be on the same page, as soon as the javascript field is populated, have the page post to the same page and then look for that passed value from the input via the appropriate request method.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the point is, doing that without understanding the life cycle of a JSP is pointless. It's unlikely to achieve anything useful.
 
Pat Flickner
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No argument there. One should understand how it all works together. The best way that helped me understand how it all interacted was turning on both debugger in the javascript functions (if used) and running the processes in debug mode for the Java portion. That way you could see exactly what was being passed from the page to the Java code. Pretty cool to see how the information is extracted. Well, if you're me, it's pretty cool.

Anyway, if the questioner and whomever else is interested are smart, they'll read the article and play around so that they do understand what's going on. Otherwise, they'll be no better than many certified people I've had the displeasure of interviewing -- lots of knowledge (memorization) and no understanding or application.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic