File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes JavaScript varible into Jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JavaScript varible into Jsp" Watch "JavaScript varible into Jsp" New topic
Author

JavaScript varible into Jsp

krish
Greenhorn

Joined: Nov 05, 2002
Posts: 5
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

Joined: Jan 10, 2002
Posts: 61106
    
  66

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

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

Joined: Nov 17, 2003
Posts: 173
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

Joined: Nov 17, 2003
Posts: 173
Quick update, in case I assumed too much:
<SCRIPT language="JavaScript">
<% out.println("var jsVarName = '" + jspVarName + "';");%>
</SCRIPT>
Pat
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

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

Joined: Nov 17, 2003
Posts: 173
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JavaScript varible into Jsp