Solved

Problems using ArrayList in JSP with a servlet and Bean interaction

Posted on 2007-12-05
15
2,179 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
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 27

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 27

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 27

Accepted Solution

by:
rrz earned 500 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 27

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 62
servlet example issue 6 46
Configure a Bean in an XML file 4 42
type mismatch (Object[] to double[] 4 33
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

856 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