• Post Reply Bookmark Topic Watch Topic
  • New Topic

Javascript accessing jsp variable  RSS feed

 
Venkat Babu
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


In my jsp I have a string variable, the javascript in that jsp should access that varialbe, don't know how I can do it? Any help. Thanks.

a.jsp

String year = (String)session.getAttribute("YEAR");

<script>
function test(){

//access year
}
</script>
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaScript can't access the JSP variable, because that exists on the server when the page is generated. You can embed the value of that variable in the JavaScript code. Something like the following should do the trick.

Leave out the double quotes if the value is numerical.
 
Venkat Babu
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great. Thanks a lot. Let me try that.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be sure that you understand why that works. Do a View Source on the page as sent to the browser, and you will see that the value was substituted on the server before the browser gets the page.

You may also find this article helpful.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!