Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Script variable in JSP

 
Priya Lavti
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Can anybody tell me how to access the Java Script variable in a JSP Java script.

Thanks in advance
 
Sravan Kumar
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The normal way you do. The page is ultimately going to be an HTML, so just use the variable, like you use it normally.



Is this what you want? Or do you want to use the variable 'a' declared in javascript as a java variable in JSP? I do not think that is possible.
 
Priya Lavti
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply but I want to use varaible 'a' in JSP as java variable. In my case the variable 'a' is declared outside the function.

<script>
var a = 5;
funtion fun()
{
a = 10;
}
</script>
 
Sravan Kumar
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either way, it is a script variable. I don't think we can use that as a java variable.
 
Priya Lavti
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then is there any way to pass the javascript variable as a request parameter. I think it can be done using HTML also.

Given is the sample code which I am trying, but it doesn't work.

Can anybody suggest anyother alternative. One thing more, I want to pass it using the anchor tags <a></a> only.

 
Sravan Kumar
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should help you
<html>
<head>
<title>JSP Page</title>

<script>
var a=10;
function fun()
{
var link = document.getElementById("link");
var url = "index.jsp?a=" + a;
link.href= url;
}
</script>
</head>

<body>
<h1>JSP Page</h1>
<a id="link" href="index.jsp" onClikk="fun();">click here</a>
</body>
</html>
 
Christopher Elkins
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also update the value of a hidden form field via javascript and then submit the form via POST method if you didn't want to pass the value in the url.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic