>> I want to assign the value of a Javascript variable to JSP Variable
you only can do it either passing via URL (like window.location.href="mypa
<%
String myvalue = request.getParameter("myva
%>
Main Topics
Browse All TopicsDear All ,
I want to assign the value of a Javascript variable to JSP Variable .. for example :
<%
Bla Bla Bla
%>
<script>
var JavaScriptVariable="hello"
</script>
<%
String JSPVariable=// The Value of JavaScriptVariable ;
%>
How can i do that ??
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>> I want to assign the value of a Javascript variable to JSP Variable
you only can do it either passing via URL (like window.location.href="mypa
<%
String myvalue = request.getParameter("myva
%>
Hi
I dont think it can not be (as i know nothing in the programming is impossible !) but i know it is not a direct solution.
redirecting is a solution but not on my case , because the value is calculated using Java Script , and i want to use it programmingly inside JSP on the same page !!!
to make the things a little bit clearer , i want to describe the problem that guide to suggest this solutions in more details:
i have a Javascript function which takes a parameter a javascript variable defined in the header , but with some additional suffix and prefix to form a new variable, for example:
<script>
// This variable is generated automatically in the form FS_<Year Value>_Q1
FS_2005_Q1=" Just an example " ;
function printSomething( value)
{
// Print The Value
}
</script>
<script>
var yearValue=// the value of the current year ;
</script>
...... Some HTML Code .....
<script>
// print the variable: FS_<The Value of the variable yearValue>_Q1
printSomthing("FS_"+yearVa
</script>
I dont think the printSomething(..) Call that i wrote is correct (Not work with me) , so how can i write it in the correct way ?
Thanks
I can
>>printSomthing("FS_"+year
is this printSomething part of Javascript or JSP ? if is Javascript you may need using the eval function, here is an example:
<script language="JavaScript">
function printSomething(v) {
document.write("Now is " + v);
alert(v);
}
var testDate = new Date();
eval("printSomething('"+te
</script>
is this what you looking for.. or ?? ..
Dear ryancys
printSomething is part of Javascript not JSP.
The problem is that the variable Name should be passed to printSomething is it self a variable !
The variable that should be passed to PrintSomething as i described above is combined as the following : FS_<Value Of Another Variable>_Q1 , for example :
printSomething(FS_<SimpleV
I can do it if <SimpleVariable> is JSP variable , but what if <SimpleVariable> is JavaScript variable ?
If it is JSP Variable i will do it like the following :
printSomething(<%="FS_"+Si
BUT , how can i dot it if SimpleVariable is JavaScript variable ???
Thanks
>>I can do it if <SimpleVariable> is JSP variable , but what if <SimpleVariable> is JavaScript variable ?
The short answer is you can't. As I explained earlier JSP variables are executed on the server before any Javascript is run.
You will need to implement your code in Java on the server rather than to do it as Javascript on the client-side. In this case it would be creating a new Date in java and then outputting th result of this into the javaScript.
Hi ryancys ,
It does not work !
let me give you the complete example :
<script>
printSomething(<%="FS_"%>2
//QuarterValue is JSP Variable either : Q1 , Q2 , Q3 , Q4
</script>
Now , how can i replace 2005 with SimpleVariable , when i directly replace it , it does not work !!
When i replace it using eval function it does not work !!
Regards,
Sorry bloodredsun ,
The recoding actually is not solution , i did not get a direct solution either from you or from ryancys , but i measure the splitting decision based on what each of you provide to me , i think ryancys was more interactive and provide me with possible solutions that i did not think about , and give me some hints to use eval function more and more in my coming JavaScript codes !!
Thanks All :)
Business Accounts
Answer for Membership
by: bloodredsunPosted on 2005-10-22 at 02:55:58ID: 15137613
You can't.
JSP variables are executed on the server, JavaScript variables are executed on the client-side(in the browser).
What you can do is put JSP variables in the JavaScript.
e.g.
<%
String JSPVariable= "Hello there";
%>
<script type="text/JavaScript">
var JavaScriptVariable="<%= JSPVariable >" ;
alert(JavaScriptVariable) ;
</script>