Learn Java for Web Development

(Tina Meador) #1
CHAPTER 4: Building a Web Application Using Struts 2 197


  1. public String searchByKeyword() {}



  2. public String home() {}



  3. public String selectedBooks(){}



  4. public String logout() {}



  5. // getters and setters



  6. }


   Line 5: responsible for displaying the login form.
 Line 7: authenticates the user against the database.
 Line 9: displays the error message, for example if the user is invalid.
 Line 11: lists all the books in the bookstore.
 Line 13: lists the books by category.
 Line 15: allows user to search the books by keyword: book title or
author’s name.
 Line 17: displays the home page when clicked on home link.
 Line 19: displays the list of selected books.
 Line 21: allows the user to log out.

Login Using Database

Listing 4-40 illustrates the executelogin() method responsible for authenticating user against the
database. For this you need to add the USER table to the data model you developed in chapter 1
using the following DDL:


CREATE TABLE USER(
ID INT NOT NULL AUTO_INCREMENT,
FIRST_NAME VARCHAR(60) NOT NULL,
LAST_NAME VARCHAR(60) NOT NULL,
USERNAME VARCHAR(60) NOT NULL,
PASSWORD VARCHAR(60) NOT NULL,
PRIMARY KEY (ID)
);


Listing 4-40. executelogin( )method in the BookController



  1. public String executelogin() {

  2. String executelogin = "failed";

  3. session = ActionContext.getContext().getSession();

  4. dao = new BookDAOImpl();

  5. user = new User();

  6. user.setUserName(getUsername());

Free download pdf