share variable value between java script and jsp

Hi, Firends:

In my old web page I used a static array to hold a set of values, and now I want to use dynamic value from datbase.

I created a bean to process database data and return resultset value to an array string.

I convert my html page into jsp page, use "usebean" tag to call specific method, after done, how can I set the

resultvaule to my script variable?

It's urgent!!great thanks!

[451 byte] By [yaying] at [2007-9-26 2:39:31]
# 1

Hi ,

you can try this.

<%

String[] dataArray = resultBean.getResultSet();

%>

<script>

var jsArray = new Array( <%for(int i = 0; i < dataArray.length; i++)

{

out.print("\"" + dataArray.trim() + "\"");

if(i == (dataArray.length - 1))

out.println("");

else

out.println(",");

}

%>);

</script>

Priyam at 2007-6-29 10:12:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi, I try the codes from Priyam, it really makes sense, but within the script tag, jsp tag does not work. <%..>,

error messages comes out in that position.

and also looks like the variabes defined in jsp can not be used in <script>..</script>. how can I do?

Does it mean the jsp tag can only be used in HTML tag?

need more comments! Thanks

yaying at 2007-6-29 10:12:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
you have to declare your array like this in jsp then only you can access it in javascript.<%!String[] dataArray = resultBean.getResultSet();%>try this.
Priyam at 2007-6-29 10:12:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hi, Jsp tags do work inside of script tags. The problem with the code sample above is a typo I believe.

dataArray.trim()

should be changed to

dataArray.trim()

The first version will cause the page not to compile because dataArray is an array of strings and does not have any methods.

You can make this code a little easire to read by changing it slightly.

<%

String[] dataArray = resultBean.getResultSet();

%>

<script>

//javascript variable

var jsArray = new Array( );

//loop through the values in the resultset and assign them to the javascript array var

<% for(int i = 0; i < dataArray.length; i++){ %>

jsArray[jsArray.length]="<%=dataArray.trim() %>";

<% } //closing the loop block%>

</script>

bobd3 at 2007-6-29 10:12:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Hi, I try the codes again, but did not work.and the error message is: " Syntax error" , position is the <%... tag inside script tag.Are you sure jsp tag can be used inside javascript ?Can you send me some similar samples for my reference ?Thanks
yaying at 2007-6-29 10:12:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
It sounds like you actually have nested <% tags. So somewhere above where you try these code samples you may already have an open <% tag.
bobd3 at 2007-6-29 10:12:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Note that in the above examples, the authors are trying to show an array reference based on a counter, i. Unfortunately this forum software uses [ i ] to indicate the start of an italic block, and discards the value.

Changing an i to a j gives:<%

String[] dataArray = resultBean.getResultSet();

%>

<script>

//javascript variable

var jsArray = new Array(<%=dataArray.length%>);

//loop through the values in the resultset and assign them to the javascript array var

<% for(int j = 0; j< dataArray.length; j++){ %>

jsArray[<%=j%>]="<%=dataArray[j].trim() %>";

<% } %> //closing the loop block

</script>

Give that a try and see if it works any better!

cafal at 2007-6-29 10:12:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

Hello,

Finally, the codes work! thanks for all the suggestion!

The most important is "J" need <%= tag too. and do not forget the double quotation mark ("") while assigning the value.

the code like following:

<jsp:useBean id="ArrayBean" class="myProj.BeanServices"

scope="page" >

</jsp:useBean>

<%! public String[] dataArray; %>

<%

dataArray = ArrayBean.getRecordsetReturn();

%>

<script language="JavaScript1.2">

var MCC = new Array(<%=dataArray.length%>);

<%for(int j =0; j < dataArray.length; j++){ %>;

MCC[<%=j%>]="<%=dataArray[j]%>";

<%}%>;

</script>

One more question:

I found on the web server, if I used the same file name (asp file and js file, html file is ok.), seems the server always used the old compiled codes ignoring the code already changed, even after re-complile. I have to change the file name again and again.

I worked on JDeveloper environment, it has a testing web server environment on my local, I am not sure if this situation will happen in the real server environment.

How can I make any new updates go into effect immidiately! Do you know web server has some configuration to handle this?

Thanks a gain!

yaying at 2007-6-29 10:12:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...