/* Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved. This software is the confidential and proprietary information of Sun Microsystems, Inc. ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with Sun. SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. CopyrightVersion 1.0 */ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import database.*; import cart.*; /** * This is a simple example of an HTTP Servlet. It responds to the GET * and HEAD methods of the HTTP protocol. This servlet calls other * servlets. This catalog calls other servlets. */ public class CatalogServlet extends HttpServlet { public void init() throws ServletException { BookDBFrontEnd bookDBFrontEnd = (BookDBFrontEnd)getServletContext().getAttribute( "examples.bookstore.database.BookDBFrontEnd"); if (bookDBFrontEnd == null) { getServletContext().setAttribute( "examples.bookstore.database.BookDBFrontEnd", BookDBFrontEnd.instance()); } } public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get the user's session and shopping cart HttpSession session = request.getSession(true); ShoppingCart cart = (ShoppingCart)session.getValue(session.getId()); // If the user has no cart, create a new one if (cart == null) { cart = new ShoppingCart(); session.putValue(session.getId(), cart); } // set content-type header before accessing the Writer response.setContentType("text/html"); PrintWriter out = response.getWriter(); // then write the data of the response out.println("" + " Book Catalog " + "" + "
" + "

 " + "

" + "Duke's " + "Bookstore" + "

" + "
" + "
 

 "); //Information on the books is from the database through its front end BookDBFrontEnd frontEnd = (BookDBFrontEnd)getServletContext().getAttribute( "examples.bookstore.database.BookDBFrontEnd"); // Additions to the shopping cart String bookToAdd = request.getParameter("Buy"); if (bookToAdd != null) { BookDetails book = frontEnd.getBookDetails(bookToAdd); cart.add(bookToAdd, book); out.println("

" + "" + "You just added " + book.getTitle() + " "+ "to your shopping cart

"); } //Give the option of checking cart or checking out if cart not empty if (cart.getNumberOfItems() > 0) { out.println("" + "" + "" + "" + "  " + "

Please choose from our selections

" + "
Check Shopping Cart  Buy your Books
"); BookDetails[] books = frontEnd.getBooksSortedByTitle(); int numBooks = frontEnd.getNumberOfBooks(); for(int i=0; i < numBooks; i++) { String bookId = books[i].getBookId(); //Print out info on each book in its own two rows out.println("" + "" + "" + "" + "" + ""); } out.println("
" + " " + books[i].getTitle() + "  " + "$" + books[i].getPrice() + "  " + "   Add to Cart  
" + "    by " + books[i].getFirstName() + " " + books[i].getSurname() + "
"); out.close(); } public String getServletInfo() { return "The Catalog servlet adds books to the user's " + "shopping cart and prints the catalog."; } }