Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Java Script variable in JSP

 
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
 
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>
 
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic