user4274735
user4274735

Reputation: 31

Printing out elements of a list using servlet

I have this list of books and I`d like to know how can I print out all of the list elements using servlet. I am sure there is some fairly easy way to do it, but I do not know how.

public List<BookInfo> listBooks() {
   EntityManager em = EMFService.get().createEntityManager();
   // read the existing entries
   Query q = em.createQuery("select m from BookInfo m");
   List<BookInfo> books = q.getResultList();
   return books;
}

Upvotes: 0

Views: 1381

Answers (2)

void
void

Reputation: 7890

more than one way. one you can use the fields of your BookInfo entity

   PrintWriter out = response.getWriter(); 
   while (books.next()) {
        String s1 = rs.getString("field1");
        out.write("<b> "+s1+ "</b><br/>");
        String s2 = books.getString("field12");
        out.write("<b> "+s2+ "</b><br/>");
    }

Upvotes: 2

SMA
SMA

Reputation: 37103

if listBooks method is called from within the Servlet, you could print by say calling a method printBooks by passing list reference itself like below:

private void printBooks(List<BookInfo> books) {
    for (BookInfo bookInfo : books) {
        System.out.println(bookInfo);//assuming you implemented toString or use logger to log into the logs
    }
}

Upvotes: 1

Related Questions