Time for action - implementing the operations

We won't go through the whole servlet implementation code. For that, I suggest you download the accompanying code for this chapter.

The servlet will provide HTTP GET operations by overriding doGet():

protected void doGet(
HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException {

Retrieve the operation parameter and prepare the response content type:

String op = req.getParameter(PARAM_OP);
resp.setContentType("text/html");
this.logger.debug(
"op = " + op + ", session = " + this.sessionId);

Then check authentication-related operations. If the operation is a login request, then it is executed:

if (OP_LOGIN.equals(op))
{
String user = req.getParameter(PARAM_USER);
String pass = req.getParameter(PARAM_PASS);
try
{
doLogin(user, pass);
htmlMainPage(resp.getWriter());
}
catch (InvalidCredentialsException e)
{
htmlLoginForm(resp.getWriter(), e.getMessage());
}
return;
}

Otherwise, if it's a request for displaying the login form, or if the session is not valid, then the login form is displayed:

else if (OP_LOGINFORM.equals(op) || !sessionIsValid())
{
htmlLoginForm(resp.getWriter(), null);
return;
}

With the authentication checks out of the way, we then check the operations and process them. The default page is the welcome page:

try {
if (op == null)
{
htmlMainPage(resp.getWriter());
}

Then, in the case of each known operation, call the appropriate response method as follows:

else if (OP_CATEGORIES.equals(op))
{
htmlCategories(resp.getWriter());
}
else if (OP_BYCATEGORY.equals(op))
{
String category = req.getParameter(PARAM_CATEGORY);
htmlByCategory(resp.getWriter(), category);
}
else if (OP_BYAUTHOR.equals(op))
{
String author = req.getParameter(PARAM_AUTHOR);
htmlByAuthor(resp.getWriter(), author);
}
else if (OP_ADDBOOKFORM.equals(op))
{
htmlAddBookForm(resp.getWriter());
}
else if (OP_ADDBOOK.equals(op))
{
htmlTop(resp.getWriter());
doAddBook(req, resp);
htmlBottom(resp.getWriter());
}

If the operation is not recognized, then just display the welcome page:

else
{
htmlMainPage(resp.getWriter());
}
}
catch (InvalidCredentialsException e)
{
resp.getWriter().write(e.getMessage());
}
}

We'll look at the details of the categories operations now (and addBook a little later).

The htmlCategories() method is called, when processing the categories operation, to display the list of currently registered categories:

private void htmlCateories(PrintWriter printWriter)
throws InvalidCredentialsException
bookshelf-servletbookshelf-servletoperations, implementing{
htmlTop(printWriter);
printWriter.println("<h3>Categories:</h3>");
printWriter.println("<ul>");
for (String category : this.service.getCategories(session))
{
printWriter.println(
"<li><a href="" + browseByCategoryUrl(category)
+ "">" + category + "</li>");
}
printWriter.println("</ul>");
htmlBottom(printWriter);
}

The methods htmlTop() and htmlBottom() are not listed here. They write boiler-plate html for the top part of the page with menu options and the bottom part respectively.

Notice that the call to this.service.getCategories() (highlighted in the previous code) is almost too casual. In fact, no additional fuss is needed! A point that will be more obvious when we give this implementation another pass using JSP in Chapter 13.

The categories are listed as links to the byCategory operation, which shows the books in a selected category.

The outcome of htmlCategories() is something like the next screenshot. Not really the fanciest web page, but it's enough to show the idea.

Time for action - implementing the operations

For convenience, the action URLs are encoded in separate methods. For example, the browseByCategoryUrl() method retrieves the action to call when requesting the byCategory operation:

private String browseByCategoryUrl(String category)
{
return "?" + PARAM_OP + "=" + OP_BYCATEGORY
+ "&" + PARAM_CATEGORY + "=" + category;
}

Clicking on the References link would kick this request off and retrieve the books in that category:

Time for action - implementing the operations

Not shown here, the htmlAddBookForm() method displays a form with the input fields for creating a new book entry:

Time for action - implementing the operations

The request for the addBook operation is directed to the addBookUrl() method:

private String addBookUrl()
{
return "?" + PARAM_OP + "=" + OP_ADDBOOK;
}

Executing the addBook operation is a matter of extracting the parameters and calling the appropriate method from BookshelfService.

private void doAddBook(
HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
String isbn = req.getParameter(PARAM_ISBN);
String category = req.getParameter(PARAM_CATEGORY);
String author = req.getParameter(PARAM_AUTHOR);
String title = req.getParameter(PARAM_TITLE);
String ratingStr = req.getParameter(PARAM_RATING);
int rating = 0;
try
{
rating = Integer.parseInt(ratingStr);
}
catch (NumberFormatException e)
{
resp.getWriter().println(e.getMessage());
return;
}
try
{
this.service.addBook(
session, isbn, title, author, category, rating);
}
catch (Exception e)
{
resp.getWriter().println(e.getMessage());
return;
}
resp.getWriter().println("Added!");
}

Have a go hero- implementing the remaining operations

Pretty straightforward, so do you think you can implement the remaining methods?

The htmlByAuthor() can be both the search and result display page. It would omit the results section when the author property is not set.

It's not worth spending any time on the aesthetics of the graphical interface. We will soon re-implement this using JSP.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.122.82