aspose file tools
The moose likes JSP and the fly likes passing a java variable from a JSP to javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » JSP
Reply Bookmark "passing a java variable from a JSP to javascript" Watch "passing a java variable from a JSP to javascript" New topic
Author

passing a java variable from a JSP to javascript

Angus Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 279
Hi,

I am retrieving a variable from a JSP like this, ${chrome} and it works. Now I have a js script and I want retrieve it from inside

Any idea?

Thanks
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 1319
    
    7

What you can is declare a java script variable that is generated by the JSP. Something like this



Let's say the value of chrome in Java is Hello World, the HTML generated will be




You can use the javascript chrome variable in your java script.
Angus Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 279
I am getting the chrome value from a the request.

Any idea?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56521
    
  14

Angus Ferguson wrote:I am getting the chrome value from a the request.

This sentence does not parse.

Please read this article to understand why JavaScript has not access to server0side JSP variables. All you can do is what has already been outlined: create markup on the server that's sent to the client for interpretation.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Angus Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 279
Any example of that, please?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56521
    
  14

A perfect example has already been posted by Jayesh.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: passing a java variable from a JSP to javascript
 
Similar Threads
session variable
How can I access JSP variable value in one jsp from adifferent jsp file
session problem in tomcat 5.0.30
set the custom tag variable
send variable