This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java array and javascript array

 
Mike Yu
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ruilin Yang
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ant Swa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ruilin Yang
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your response. My code is correct. The javascript error I got is due to caching.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic