?
Solved

Problems using ArrayList in JSP with a servlet and Bean interaction

Posted on 2007-12-05
15
Medium Priority
?
2,182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20415606
You might try

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

Expert Comment

by:objects
ID: 20415650
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
ID: 20415667
" + 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 

Author Comment

by:VoodooFrog
ID: 20415684
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
ID: 20415694
> " + 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
ID: 20415767
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
ID: 20415809
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
 
LVL 92

Expert Comment

by:objects
ID: 20415908
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 28

Expert Comment

by:rrz
ID: 20416028
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
ID: 20416067
>>and bookList is an ArrayList containing String[]s,

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

Author Comment

by:VoodooFrog
ID: 20429762
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 28

Expert Comment

by:rrz
ID: 20430159
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
ID: 20430303
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 28

Accepted Solution

by:
rrz earned 2000 total points
ID: 20430539
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 28

Expert Comment

by:rrz
ID: 20442603
You should have split the points.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 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