File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Java array and javascript array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Java array and javascript array" Watch "Java array and javascript array" New topic
Author

Java array and javascript array

Mike Yu
Ranch Hand

Joined: Nov 17, 2001
Posts: 175
Hi,
In JSP, for some reason, I need to assign the value of a Java String array to a Javascript array. Part of my code is as follows:
<%
String[] str1 = //code to assign values to its elements;
String[] str2 = //code to assign values to its elements;
//str1 and str2 arrays have the same length.
int strLength = str1==null?0:str1.length;
%>
<script language="javascript">
var strA = new Array(<%=strLength%> ;
var strB = new Array(<%=strLength%> ;
<%for(int i=0; i<strLength; i++){%>
strA[<%=i%>] = "<%=str1[i]%>";
strB[<%=i%>] = "<%=str2[i]%>";
<%}%>
</script>
When I run the JSP page, I got a javascript error. I know this error is caused by the code in the for loop. If I replace <%=str1[i]%> and <%=str2[i]%> with a javascript string, for example, "test1", "test2", then it is fine.
Does someone know what is wrong within the above for loop?
[ September 11, 2002: Message edited by: Mike Yu ]

Thanks,<br />Mike
Ruilin Yang
Ranch Hand

Joined: Jan 06, 2002
Posts: 150
Your code:
<%for(int i=0; i<strLength; i++){%>
strA[<%=i%>] = "<%=str1[i]%>";
strB[<%=i%>] = "<%=str2[i]%>";
<%}%>

I would use:
<%for(int i=0; i<strLength; i++){%>
strA[i] = "<%=str1[i]%>";
strB[i] = "<%=str2[i]%>";
<%}%>
Sanjeev Kaushik
Ranch Hand

Joined: Aug 01, 2002
Posts: 105
Hi
I have tried you code in weblogic and it worked fine. Here is jsp file which I copied from your code
<html>
<head>
<%
String[] str1 = {"sanjeev","kaushik"};//code to assign values to its elements;
String[] str2 = {"eyesmax","delhi"};//code to assign values to its elements;
//str1 and str2 arrays have the same length.
int strLength = str1==null?0:str1.length;
%>
<script language="javascript">
var strA = new Array(<%=strLength%> ;
var strB = new Array(<%=strLength%> ;
<%for(int i=0; i<strLength; i++){%>
strA[<%=i%>] = "<%=str1[i]%>";
strB[<%=i%>] = "<%=str2[i]%>";
<%}%>
</script>
</head>
<body>
testing jsp
</body>
</html>

and following the html source on the client side
<html>
<head>
<script language="javascript">
var strA = new Array(2);
var strB = new Array(2);
strA[0] = "sanjeev";
strB[0] = "eyesmax";
strA[1] = "kaushik";
strB[1] = "delhi";
</script>
</head>
<body>
testing jsp
</body>
</html>
I suppose that was the only required result.
Regards,
Sanjeev


Sanjeev Kaushik
Ant Swa
Greenhorn

Joined: May 28, 2002
Posts: 13
yes, nothing wrong with Mike's code.
Only change i did was initialising the two arrays
i.e.
String[] str1 = { "ssss" , "rrrr"};
String[] str2 = { "eeeee", "ffff"};
and it works fine.
But Ruilin,
What u think is wrong. By writing
strA[i] = "<%=str1[i]%>";
strB[i] = "<%=str2[i]%>";
if u can do this for the variable i, which is a server side variable, u will be able to do it for the str1 and str2 also. Then we need not write this much code for this task...


Anton
Ruilin Yang
Ranch Hand

Joined: Jan 06, 2002
Posts: 150
You guys are right. I made mistake.
Nescape browser has some useful script debug functionality. You can use it to locate where is the problem for script.
[ September 12, 2002: Message edited by: Ruilin Yang ]
Mike Yu
Ranch Hand

Joined: Nov 17, 2001
Posts: 175
Thank you all for your response. My code is correct. The javascript error I got is due to caching.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java array and javascript array