Problems using ArrayList in JSP with a servlet and Bean interaction

I have been working with an ArrayList and am getting an odd output in JSP.  I have tried a variety of looping structures and attempts to pull the ArrayList out and can not get the information out that I put into it.   The structure is a servlet gets information from a database and compiles an ArrayList of Strings that it passes to abean, which the JSP page calls and iterates through.


the resuts on JSP page at run time:

[Ljava.lang.String;@1b104d7
[Ljava.lang.String;@b4d39c
[Ljava.lang.String;@1b0d33c
[Ljava.lang.String;@1fc468e

Here is the code i the JSP:



<c:forEach var="i" begin="1" end="${bookBean.numCopies}">

<LI>${bookBean.books[i-1]}</LI>

</c:forEach>


Bean reference:

public ArrayList<String> getBooks(){

return this.bookList;

}


public void setBooks(ArrayList<String> bookName){

this.bookList = bookName;

}



Call from the servlet



ArrayList booklist = getBooks(isbn);


// Output the results of the search


// startContent(servletOut, " Books Available ");

if (booklist == null) {

RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/BuyFail.jsp");

dispatch.forward(request, response);


} else {

BookBean tester1 = new BookBean();

tester1.setBooks(booklist);

tester1.setNumCopies(booklist.size());

request.setAttribute("bookBean", tester1);

RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/Buy2.jsp");

dispatch.forward(request, response);

}
VoodooFrogAsked:
Who is Participating?
 
rrzConnect With a Mentor Commented:
Here is a test JSP.  
<%@ page import="java.util.*, java.io.*" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
      HashMap<String,String[]> books = new HashMap<String,String[]>();
      String[] book1= {"a","b","c"};
      String[] book2= {"d","e","f"};
      books.put("book1Title",book1);
      books.put("book2Title",book2);
%>
<c:forEach items="<%=books%>" var="item">
                 ${item.key}
                 <c:forEach items="${item.value}" var="current">
                                                                 <LI>${current}</LI>
                 </c:forEach >
</c:forEach>

Open in new window

0
 
CEHJCommented:
You might try

out.println("<LI>" + Arrays.toString(${bookBean.books[i-1]}) + "</LI>");
0
 
objectsCommented:
your code above is fine, the proiblem appears to be when you you are populating your books list.
check that code
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
VoodooFrogAuthor Commented:
" + Arrays.toString([Ljava.lang.String;@79ac92) + "")  is the result from the change there.  it seems like it is creating a tring out of the object reference instead of the referenced string.  
0
 
VoodooFrogAuthor Commented:
the population of the booklist works as follows (tested it this way before working on the JSP)

servletOut.println("<tr>");
    String info [] = (String [])booklist.get(i);
    for (int j = 0; j < 5; j++) {
      servletOut.println("<td>" + info[j] + "</td>");
    }

my thought was that JSP did the casting itself -- is there a way to convert that servlet code to JSP easily?  I am fairly new at both servlets and JSP's
0
 
objectsCommented:
> " + Arrays.toString([Ljava.lang.String;@79ac92) + "")  is the result from the change there.  it seems like it is creating a tring out of the object reference instead of the referenced string.  

Arrays.toString is not going to change anything, its for converting arrays to strings and you are outputing strings.
what you have already is fine.

instead check how you are adding the strings to the array, looks like you doing a toString() on an array.
0
 
VoodooFrogAuthor Commented:
my population code:  

ArrayList<String> bookdata = (ArrayList)db.get(isbn);
            if (bookdata == null) {
                bookdata = new ArrayList();
                bookdata.add(info);
                db.put(isbn, bookdata);
                count = 1;
            } else {
                bookdata.add(info);      // This also changes the bookdata object in db
                count = bookdata.size();
            }
0
 
CEHJCommented:
You're trying to print a String[]. You can only do that in Java the way i mentioned (though i got the code wrong)
0
 
objectsCommented:
ArrayList<String> bookdata = (ArrayList)db.get(isbn);
            if (bookdata == null) {
                bookdata = new ArrayList();
                bookdata.add(info);

should be more like:

List<String> bookdata = (List)db.get(isbn);
if (bookdata == null) {
   bookdata = Arrays.asList(info);
0
 
rrzCommented:
If CEHJ is right, and bookList is an ArrayList containing String[]s,  then you could try something like  
<c:forEach items="${bookBean.books}" var="item">
     <c:forEach items="${item}" var="current">
        <LI>${current}</LI>
     </c:forEach >
</c:forEach>
0
 
CEHJCommented:
>>and bookList is an ArrayList containing String[]s,

Of course it should be ArrayList<Book>, per your other example
0
 
VoodooFrogAuthor Commented:
I think that there is a problem more in the environment, because I have been able to run very similar code on a different computer -- I am still working on this as well.  Haven't been able to successfully implement any of the proposed solutions.  
0
 
rrzCommented:
In your bean, "bookList" is an ArrayList containing Strings. In your Servlet booklist is an ArrayList containing String arrays.   Why don't you describe your data and we will help you design the structure.
0
 
VoodooFrogAuthor Commented:
Data structure in this is just a group of store items that are available -- I was using books in this one.  The context is where all a given book is available -- picture a shopzilla sort of arrangement.  So the desired output is:

Book X is available at:
Location1
Location2
Location3
etc

so an array of strings is what I am after.  I used the same data type in the bean and the servlet so I thought that was done correctly.  
0
 
rrzCommented:
You should have split the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.