Learn Java for Web Development

(Tina Meador) #1

98 CHAPTER 2: Building Web Applications Using Servlets and JSP


   Line 8: The action is not null, and the value of the action is category.
 Lines 15 to16: The helper method findAllBooks(request, response) is invoked,
the URL is reconstructed to point to listOfBooks.jsp, and RequestDispatcher
forwards to the view provided to the RequestDispatcher in the form of a URL.

Listing 2-37 shows the helper method findAllBooks(request, response) in the BookController.


Listing 2-37. findAllBooks( ) in BookController


1.private void findAllBooks(HttpServletRequest request,
2.HttpServletResponse response) throws ServletException, IOException {
3.try {
4.BookDAO bookDao = new BookDAOImpl();
5.List bookList = bookDao.findAllBooks();
6.request.setAttribute("bookList", bookList);
7.
8.} catch (Exception e) {
9.System.out.println(e);
10.}
11.}


   Lines 5 to 6: A list of all books is obtained from the database using the
findAllBooks() method on the DAO and is set as an attribute in the request.

Searching the Books by Keyword

You can search the books by the author’s name or a keyword in the book’s title, as illustrated in
Figure 2-39.


Figure 2-39. Searching books by keyword

Free download pdf