1

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)%>?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.