Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have 4 arraylist in jsp and I want to get elements of lists from javascript function.

it looks like there is no problem getting one element from jsp arraylist.

But I don't know how to move multiple or all elements from jsp arraylist to javascript code.

below is my code

<script>
    function makeTable(){
    for(c=0;c<row_num;c++){
        row[c]=document.createElement('tr'); 
        for(k=0;k<cell_num;k++) {
            cell[k]=document.createElement('td');
            cont = document.createElement('a');
            cont.href="./bbs_view.jsp?count=" + c;
            cont.innerHTML ="<%=title.get(0)%>";
            cell[k].appendChild(cont);
            row[c].appendChild(cell[0]);
        }
    }
}
</script>

as you can see, above function can get only one element from jsp arraylist.
is there any way to replace <%=title.get(0)%> to
something like <%=title.get( javascript var c)%>?

share|improve this question
up vote 1 down vote accepted

Emit a javascript array within your scriplet (assumin titles is a java collection of Strings) and use the js counterpart of your array.

<script>

var jsArray = [];

<% int i = 0; foreach (String iterat : titles) { %>

jsArray[<%= i %>] = '<%= iterat %>';

<% i++; } %>
</script>

Is roughly unelegant (better have this in a custom tag, but should work).

BEWARE: typed on the fly, can have syntax typos in ^^

BigMike

share|improve this answer

No.

JSP runs on the server. It outputs some text. The browser interprets that text as JavaScript. There is no path back.

Instead, use a JSON encoder to provide the array in a JavaScript friendly format. Use that in your script so you get a JavaScript array, then loop over that.

share|improve this answer

Ok.. I finally made it.

for those who will have same question like me, I would like to leave my code.

<%@import="java.sql.*, java.util.ArrayList"%>

<%  
class bbsData{
    String title;
    String date;
    String body;
    int count;
}

ArrayList<bbsData> bbslist = new ArrayList<bbsData>();
    try{
     rs=stmt.executeQuery("select * from bbsdata");
     while(rs.next()){
        bbsData bbsOb = new bbsData();
        bbsOb.date = rs.getString(1);
        bbsOb.title= rs.getString(2);
        bbsOb.body = rs.getString(3);
        bbsOb.count = Integer.parseInt(rs.getString(4));
        bbslist.add(bbsOb);
    }    
    stmt.close();
    Conn.close();        
}catch(Exception e){
    out.println(e);
} 
<script>

var jsArray = [];

function dbData(date, title, body, count){  
   this.date = date;
   this.title = title;
   this.body = body;
   this.count = count;
}
function setJsArry(){
    <% int i = 0;
       for (bbsData iterat : bbslist) {
           iterat = new bbsData();
           iterat = bbslist.get(i);
     %>
    date = '<%= iterat.date %>'; 
    title = '<%= iterat.title %>';
    body = '<%= iterat.body %>';
    count = '<%= iterat.count %>';
    jsArray[<%=i%>] = new dbData(date, title, body, count);
       <%i++; } %>

}
function makeTable(){
    setJsArry();
    for(c=0;c<row_num;c++){
    row[c]=document.createElement('tr'); 
       for(k=0;k<cell_num;k++) {
          cell[k]=document.createElement('td');
          cont = document.createElement('a');
          cont.href="./bbs_view.jsp?count=" + c;
          cont.innerHTML =jsArray[c].title;
          cell[k].appendChild(cont);
          row[c].appendChild(cell[k]);
      }
  }
}
</script>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.