wood burning stoves
The moose likes JSP and the fly likes how to use jsp variables in javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how to use jsp variables in javascript" Watch "how to use jsp variables in javascript" New topic

how to use jsp variables in javascript

satyakiran yarlagadda

Joined: Apr 29, 2005
Posts: 1
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

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

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.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

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.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jayaraj Sekaran

Joined: Apr 17, 2005
Posts: 5

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.

SCJP 1.4<br />SCWCD 1.4<br />WSAD 5.0
I agree. Here's the link: http://aspose.com/file-tools
subject: how to use jsp variables in javascript
It's not a secret anymore!