• Post Reply Bookmark Topic Watch Topic
  • New Topic

use javascript variable in JSP?

 
brisk rook
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I access JavaScript variables in a JSP page?
eg:
<script type="text/javascript">
var name = prompt("Please enter your name","")
if (name != null && name != "")
{
document.write("Hello " + name)
}
</script>
is there any way that i can use variable "name" in the same JSP page...
<% some code here goes which needs the "var name" of java script %>

thanks in advance
br
 
Dmitriy Pavlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Concepts of client and server side scripting can not allow you to use variables from client side on the server side. The question is the JSP is processing on the server side and AFTER that the result of it work (usually HTML and JavaScript) is sending to client and browser interpret it. So JSP doesn't use the variable of javascript. You ougth to learn the concepts of this interaction.
But you may send the javascript variable to ANOTHER JSP (fe with anchor or form) as parameter and process it in JSP.
 
Kris Nelson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because JSPs are compiled on the server and sent to the client as html, it is impossible for the JSP (on the server) to know about JavaScript variables (on the client). In your example, you could use a the result of the prompt box to dynamically write a form with the result as a hidden value. That page could then redirect to itself (assuming it's a JSP) and check for the existance of the hidden form value.

------------------
WebNelly.com
Java/XML Web Development
Check it out!
http://www.webnelly.com
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!