This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
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

Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 331
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: 2270
    
  28

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.
Isaac Ferguson
Ranch Hand

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

Any idea?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60732
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Isaac Ferguson
Ranch Hand

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

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60732
    
  65

A perfect example has already been posted by Jayesh.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing a java variable from a JSP to javascript
 
Similar Threads
send variable
session problem in tomcat 5.0.30
session variable
set the custom tag variable
How can I access JSP variable value in one jsp from adifferent jsp file