Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Java Script variable in JSP

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic