Solved

Problems using ArrayList in JSP with a servlet and Bean interaction

Posted on 2007-12-05
15
2,180 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
type mismatch (Object[] to double[] 4 41
spring jars download 1 53
how to debug htl and js pages 8 58
Java basic valueOf question 1 35
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

730 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