Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use jsp variables in javascript

 
satyakiran yarlagadda
Greenhorn
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

sincerely
satya
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate 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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 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.
 
Jayaraj Sekaran
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You can use hidden variables for solving this problem.

Use it like this:

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

And inside the Java script, access the value like this

<script>
var obj = document.getElementById("xyz");
</script>

Here obj.value will give you the value you expected. Please try this and let me know.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic