java script and jsp array

i could not solve this my head turned wight :$


java script function:

String[] str = request.getParameterValues("str");<<<<array in previous page as response.sendRedirect("keyword.jsp?str="+str);

function validate()
{
    alert("Aaaaaa");
  var array1;
    var j=0;
<%
for(int i = 0; i < str.length; i++)
{
  out.print("\""+str[i]+"\"");
  %>
   array1[j]=<%=str[i]%>;
   j++;
 <%
}
%>
    var i;
    for (i=0;i<array.length;i++)
    {
.......}


thank you :)))
aseeladAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rrzCommented:
Are you trying to populate the javascript array with your JSP array ?
<% 
   String[] str = {"1111","2222","3333"};
%>
<html>
<head>
<script>
function validate()
  {
    var array1 = new Array();
<%
  for(int i = 0; i < str.length; i++)
   {
    out.println("array1[" + i + "]=\"" + str[i] + "\";");
   }
%>
  alert(array1);
}
</script>
</head>
<body onload="validate()">
testing
</body>
</html>

Open in new window

aseeladAuthor Commented:
not populate but assign the the jsp array to the java script array
aseeladAuthor Commented:
I just need to traverse the array in the java script code the array that came from another web page
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

rrzCommented:
I think what I posted will work for you. Just change from my test array to your parameter values. See below.
<% 
   String[] str = request.getParameterValues("str");
%>
<html>
<head>
<script>
function validate()
  {
    var array1 = new Array();
<%
  for(int i = 0; i < str.length; i++)
   {
    out.println("array1[" + i + "]=\"" + str[i] + "\";");
   }
%>
  alert(array1);
}
</script>
</head>
<body onload="validate()">
testing
</body>
</html>

Open in new window

aseeladAuthor Commented:
There is a problem when I pass the array from the other page it did not show me the elements in the array its show me some weird character
rrzCommented:
Sounds like a encoding problem. Please show us the array elements and your code. Also show us  the source code sent to the browser.
aseeladAuthor Commented:

the array when i pass it, the url :
str=[Ljava.lang.String;@1643b92

-----------------------------------
the first jsp page :
String[] str={"notebook","note","ink"};
response.sendRedirect("keyword.jsp?str="+str);
-----------------------
keyword.jsp:
String[] str = request.getParameterValues("str");
and the java script function.
-------------------------------
thank you
rrzCommented:
>str=[Ljava.lang.String;@1643b92  
That is the hashcode for the array object.  
Please try using
String[] str={"notebook","note","ink"};
response.sendRedirect("keyword.jsp?str="+ str[0] + "&str=" + str[1] + "&str=" + str[2]);
aseeladAuthor Commented:

but if do not now the length of the array ? i loop throw the array and assign it ?


other issue in the second page (keyword.jsp) how can i take the vaules ?string str = request.getParameter(str) will only return only one value?


thank you
newalopezCommented:
If you want only to pass them to the other page, as the full array, you could send them hidden as a session value.

response.getSession(false).setAttribute("myarray", str);

or then, you can build your url in java, and the put it on the page, easier than writing the code step by step...

StringBuffer buffer = new StringBuffer("keyword.jsp?");
for (String tmp: str) {
  buffer.append("str=");
  buffer.append(tmp);
}
response.sendRedirect(buffer.toString());
rrzCommented:
>but if do not now the length of the array ? i loop throw the array and assign it ?  
Yes.  
But newalopez has a good idea. It would be easier to use a session-scoped object. That way you don't have to send it to the browser and then back to the server  

>response.getSession(false).setAttribute("myarray", str);  
Don't use this.  
Use the following:
-----------------------------------
the first jsp page :
String[] str={"notebook","note","ink"};
session.setAttribute("str", str);
String encodedURL = response.encodeRedirectURL("keyword.jsp");
response.sendRedirect(encodedURL);
-----------------------
keyword.jsp:
String[] str =(String[]) request.getAttribute("str");
and the java script function.
-------------------------------

If you don't really need to redirect, you could just use a forward.  

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.