Learn Java for Web Development

(Tina Meador) #1

198 CHAPTER 4: Building a Web Application Using Struts 2



  1. user.setPassword(getPassword());

  2. setUser(user);

  3. if (dao.isUserAllowed(user)) {



  4. setCategoryList(dao.findAllCategories());

  5. session.put("username", username);

  6. session.put("categoryList", getCategoryList());

  7. executelogin = "success";

  8. }

  9. else {

  10. addActionError(getText("error.login"));

  11. return "error";

  12. }

  13. // return result;

  14. return "executelogin";

  15. }


Figure 4-24 illustrates the USER table.


Figure 4-24. USER table


   Line 9: The user is authenticated against the database. As you can see in the
Line 9 replaces the hard-coded username and password in the Listing 4-31 by
invoking isUserAllowed() on the DAO The isUserAllowed() method selects
the username and password from the USER table (as shown in Figure 4-24) in
the resultset based on the username and password entered in the login form.
 Line 11-14: If the user is valid, categories are retrieved from the database, and
the list of categories is stored in the session.
 Lines 16-19: Line 16 sets the valid boolean variable to true if the resultset
contains the username and password If the user is not valid, the String error is
returned.

Displaying Categories Retrieved from the Database

Listing 4-41 illustrates the menu that shows the categories returned from the database. In Listing 4-40,
you retrieved the categories and stored them in the session for a valid user. These categories will be
displayed in menu.jsp.

Free download pdf