Java Reference
In-Depth Information
Listing 2-29. doGet( ) and doPost( ) in BookController
1.protected void doGet(HttpServletRequest request,
2.HttpServletResponse response) throws ServletException, IOException {
3.doPost(request, response);
4.}
5.
6.protected void doPost(HttpServletRequest request,
7.HttpServletResponse response) throws ServletException, IOException {
8.String base = "/jsp/";
9.String url = base + "home.jsp";
10.String action = request.getParameter("action");
11.String category = request.getParameter("category");
12.String keyWord = request.getParameter("keyWord");
13.if (action != null) {
14.switch (action) {
15.case "allBooks":
16.findAllBooks(request, response);
17.url = base + "listOfBooks.jsp";
18.break;
19.case "category":
20.findAllBooks(request, response);
21.url = base + "category.jsp?category=" + category;
22.break;
23.case "search":
24.searchBooks(request, response, keyWord);
25.url = base + "searchResult.jsp";
26.break;
27.
28.}
29.}
30.RequestDispatcher requestDispatcher = getServletContext()
31..getRequestDispatcher(url);
32.requestDispatcher.forward(request, response);
33.}
Line 3 : The doPost() method is called from the doGet() method.
Line 9 : This line constructs the URL that points to the home page view
( home.jsp ).
Line 10 : This line gets the action parameter from the request. But since this is
a home page, there is no associated action parameter, so the variable action
is null .
Lines 13 to29 : The code block from lines 13 to 29 is skipped as the action is
null . If the action was not null , the URL would have been reconstructed to
point at a different view depending on whether the action value is allBooks or
category or search .
Line 32 : The RequestDispatcher forwards to the view name in the URL
on line 31.
 
Search WWH ::




Custom Search