private List<Book> getBooksFromRequest(HttpServletRequest request) { List<Book> list = null; String categoryString; String searchString; Category category; categoryString = request.getParameter(CATEGORY_CMD); searchString = request.getParameter(SEARCH_CMD); if (categoryString != null && !categoryString.isEmpty()) { if (categoryString.equalsIgnoreCase(ON_SPECIAL_CMD)) { list = bookDatabaseAccessor.getSpecialBooks(); } else { category = Category.valueOf(categoryString); list = bookDatabaseAccessor.getBooks(category); } } else if (searchString != null && !searchString.isEmpty()) { list = bookDatabaseAccessor.getBookByKeyword(searchString); } return list; }
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String command; User user; String url = MAIN_JSP; command = request.getParameter(COMMAND); System.out.println("Command: " + command); if (command != null) { if (command.equalsIgnoreCase(CREATE_USER_CMD)) { user = createUserFromRequest(request); user.validateUser(); if (user.isUserValid()) { userDatabaseAccessor.insertUser(user); url = MAIN_JSP; } else { url = USER_PROFILE_JSP; } request.getSession().setAttribute(SESSION_USER, user); } else if (command.equalsIgnoreCase(UPDATE_USER_CMD)) { user = createUserFromRequest(request); user.validateUser(); if (user.isUserValid()) { userDatabaseAccessor.updateUser(user); @SuppressWarnings("unchecked") List<Book> list = (List<Book>) request.getSession().getAttribute(SESSION_LIST); if (list == null) { list = bookDatabaseAccessor.getSpecialBooks(); request.getSession().setAttribute(SESSION_LIST, list); } url = MAIN_JSP; } else { url = USER_PROFILE_JSP; } request.getSession().setAttribute(SESSION_USER, user); } else if (command.equalsIgnoreCase(DISPLAY_MAIN_PAGE_CMD)) { List<Book> list = getBooksFromRequest(request); url = MAIN_JSP; request.getSession().setAttribute(SESSION_LIST, list); } } System.out.println("url: " + url); System.out.println(getServletContext().toString()); RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url); dispatcher.forward(request, response); }