This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JSP and the fly likes Java Script variable in 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 "Java Script variable in JSP" Watch "Java Script variable in JSP" New topic
Author

Java Script variable in JSP

Priya Lavti
Ranch Hand

Joined: Oct 11, 2004
Posts: 41
Hi all,

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

Thanks in advance


-Priya
SCJP 5.0
Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121
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.


keep smilin :: sravan<br /><a href="http://sravanpens.blogspot.com" target="_blank" rel="nofollow">I scribble here</a>
Priya Lavti
Ranch Hand

Joined: Oct 11, 2004
Posts: 41
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

Joined: Sep 11, 2005
Posts: 121
Either way, it is a script variable. I don't think we can use that as a java variable.
Priya Lavti
Ranch Hand

Joined: Oct 11, 2004
Posts: 41
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

Joined: Sep 11, 2005
Posts: 121
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

Joined: Oct 26, 2004
Posts: 45
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.


Christopher Elkins, SCJP Java 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Script variable in JSP