• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
  • Tim Holloway
  • Carey Brown
  • salvin francis

how to use jsp variables in javascript

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

i have a doubt about accessing a jsp variable in java script, the problem is as follows

In my html document, i have jsp code to fetch the data from the database(mysql) and load the values of each record in an object and add the object to the arraylist, now after this is done i have to put the values inthe form of a tree structure(jtree) in my html page for this i am using java script, noe i have to use the arraylist values in the java script, how can i do that can any one help me out in this and send any sample code if possible.

Thanks for u'r response

Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All variables created in serverside code can only be accessed by clientside code if you render them as part of the HTML page your clientside code runs on. So if you want you "pass" an array of values, you really need to create a JavaScript array clientside and populate it with the values of your array. Then access that array from your JavaScript code.
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPS/Servlets write a stream of text to the browser.
If you want to use the values of your java variables on the client as Javascript, you have to print their values into the the javascript declaration.

If you look at the HTML source from your browser (as you should always do) you will see:

The Java code is gone. All that remains is the text that your JSP code wrote. The browser will take this and interpret it as a Javascript function. It will create the ImAJavascriptVariable variable an initialize it with value of the string literal ('Bob') that it found in the text. The Javascript interpreter has no idea that that the string it's using was the results of some JSP code that printed the value of a Java variable. It just knows that the literal 'Bob' was printed there.

Java and Javascript are two different languages that get executed on two different machines. They do not interact in real time.
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You can use hidden variables for solving this problem.

Use it like this:

<input type="hidden" name="xyz" value='<%=request.getParameter("var") %>' >
<input type="hidden" name="xyz" value='<%= Server side variable %>' >

And inside the Java script, access the value like this

var obj = document.getElementById("xyz");

Here obj.value will give you the value you expected. Please try this and let me know.
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
professionally read, modify and write PDF files from Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!