Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaScript varible into Jsp

 
krish
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
I need to put a javascript varible(which is dynamic) into jsp session.
Can any one help....
-Thanks
Krish
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64844
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch krish!
You'll find this forum a great place to seek help on JSP pages, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
Thanks!
bear
JSP Forum Bartender
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64844
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now as to your question:
Since Javascript executes on the client, it has no access to the session. What you will need to do is to submit a request to the server (either to a JSP or a servlet, preferably the latter) with the value you want to store as a request parameter.
The easiest way to do this is probably to submit a form with a hidden input that you've set to the value of the JS variable.
bear
[ December 31, 2003: Message edited by: Bear Bibeault ]
 
Pat Flickner
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, good heavens. I'm very surprised at the responses since we do this all the time:
Between the <% and %>:
out.println("var variableName = " + jspVarName + ";");
Works like a charm.
Pat
 
Pat Flickner
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick update, in case I assumed too much:
<SCRIPT language="JavaScript">
<% out.println("var jsVarName = '" + jspVarName + "';");%>
</SCRIPT>
Pat
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64844
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat, your solution assumes that the question regards getting server-side data into a Javascript variable.
The question, rather, was how to get client-side Javascript data into a server-side session.
bear
 
Pat Flickner
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arghhh! You're absolutely correct. I read his message three times and still misread it. And you are correct that you would have to pass the variable into a jsp then after the ?.
Pat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic