jQuery in Action, 2nd edition*
The moose likes JSP and the fly likes Assign Javascript value to JSP var Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Assign Javascript value to JSP var" Watch "Assign Javascript value to JSP var" New topic
Author

Assign Javascript value to JSP var

sridhar lakka
Ranch Hand

Joined: Jan 02, 2007
Posts: 109
Hi All,
Thanks a lot in advance.
I have some value in javascript and I want to assign that value to a jsp variable.
I know how to assign a JSp value to a javascript variable but I am not getting any idea on this.

I have a code like below
<Script language='javascript'>
var x=5;
</script>
And I want to set this x value in seesion attribute,like
<%
session.setAttribute("XJavascript",x)---this should be javascript value
%>

Regards,
Sree
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

There is not any direct way to apply javascript value into JSP, because javascirpt run on client side and the JSP is server side language.

the only way I know is by using a hidden fields,


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218

This wont compile as x is not defined, Here x should be a java variable( not java script) of type Object.

What exactly are you trying to do, give the scenario, maybe there is some another way of doing it.
Krithika Srinath
Ranch Hand

Joined: Apr 11, 2006
Posts: 52
As Saif has mentioned, the only way to achieve this as per my knowledge is to use hidden fields along with your form. For e.g.



then in your javascript set the value as:



This should work.
 
GeeCON Prague 2014
 
subject: Assign Javascript value to JSP var