Solved

Problems using ArrayList in JSP with a servlet and Bean interaction

Posted on 2007-12-05
15
2,175 Views
Last Modified: 2013-11-24
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);

}
0
Comment
Question by:VoodooFrog
  • 5
  • 4
  • 3
  • +1
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You might try

out.println("<LI>" + Arrays.toString(${bookBean.books[i-1]}) + "</LI>");
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
your code above is fine, the proiblem appears to be when you you are populating your books list.
check that code
0
 

Author Comment

by:VoodooFrog
Comment Utility
" + 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
 

Author Comment

by:VoodooFrog
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> " + 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
 

Author Comment

by:VoodooFrog
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 
LVL 27

Expert Comment

by:rrz
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>and bookList is an ArrayList containing String[]s,

Of course it should be ArrayList<Book>, per your other example
0
 

Author Comment

by:VoodooFrog
Comment Utility
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
 
LVL 27

Expert Comment

by:rrz
Comment Utility
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
 

Author Comment

by:VoodooFrog
Comment Utility
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
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
Comment Utility
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
 
LVL 27

Expert Comment

by:rrz
Comment Utility
You should have split the points.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now