Chapter 7 Tutorials

Chapter 7 Tutorials

Account Login

  1. Create a new package named ch7.accountLogin in the project that was created in the Chapter 1 Tutorials.
    1. Copy all the files from ch5.persistentData into the new package.
    2. Rename the bean to RequestDataAccount.java
    3. Refactor all the files that reference the bean.
  2. Modify the bean.
    1. Add an account number property.
    2. Validate that the account number is not null and starts with two letters and is followed by three digits.
    3. The account number should have a column in the database.
  3. Create the Login.jsp file.
    1. Add a form.
    2. Add an input element for the acount number.
    3. Initialise the account number with the data from the bean.
    4. Add a button for logging in.
  4. Modify the controller helper.
    1. Create a login method.
      1. If the request is a GET request, show the login page without any errors. This will prevent the error showing when the user accesses the site for the first time.
      2. If the account number has the correct format, do the following
        1. Clear the errors.
        2. Try to read the account number from the database.
        3. If there is data, set it to the bean in the controller helper.
        4. The next page should be the edit page.
      3. If the account is not valid
        1. The next page should be the login page.
        2. Display an error for the account number.
        3. Make this the default method (remove default from the edit method).
    2. Modify the doGet method so that the default method is the login method.
    3. Modify the jspLocation method with the path to the JSPs.
  5. Modify web.xml.
    1. Add an init parameter named create to the servlet definition.
    2. Add a servlet mapping.
  6. Modify index.jsp
    1. Add a link to the new controller.

Account Removal

  1. Create a new package named ch7.accountRemoval in the project.
    1. Modify the controller helper from the account login package. Do not make a copy of it, modify the actual controller helper.
      1. Be sure that the doGet method is public.
      2. Be sure that the doPost method is public.
    2. Create a new controller helper that extends the account login controller helper. Only three methods need to be defined.
      1. Add an import for the account login bean.
      2. Create a constructor that calls the constructor of the super class.
      3. Override the jspLocation method; set the path to the JSPs.
      4. Create a new method for removing the bean from the database.
        1. Remove the bean from the database.
        2. Make the bean point to a new bean.
        3. Go to the login page.
    3. Copy the controller from the account login package.
    4. Do not copy the bean.
    5. Copy the JSPs from the account login package.
      1. Modify the process page so that it has a new button for removing the bean from the database.
  2. Modify the web.xml file.
    1. Add a servlet definition.
    2. Add a servlet mapping.
  3. Modify index.jsp
    1. Add a link to the new controller.

Account Cookie

  1. Create a new package named ch7.accountCookie in the project.
    1. Modify the controller helper from the account login package. Do not make a copy of it, modify the actual controller helper.
      1. Be sure that the doGet method is public.
      2. Be sure that the doPost method is public.
    2. Create a new controller helper that extends the account login controller helper.
      1. Add an import for the account login bean.
      2. Create a constructor that calls the constructor of the super class.
      3. Override the jspLocation method; set the path to the JSPs.
      4. Create a default method.
        1. If there is not a cookie named "account", then go to the login page.
        2. If there is a cookie named "account", then
          1. Use the value of the cookie to access the database.
          2. If there is data for the account number, then use the bean as the bean for the controller helper.
          3. Go to the edit page.
      5. Override the doGet method. It does the same as the super class, except it always goes to the default method.
      6. Create a new user method.
        1. Set the bean to a new bean.
        2. Go to the login page.
      7. Create a process method.
        1. This will do the same as the super class process method, except that it will also add a bean named "account" to the response.
        2. The value of the bean will be the account number.
    3. Modify the JSPs.
      1. Add a new user button to the edit page. When this button is clicked, the cookie will not be read and a new bean will be created.
      2. Modify the process page.
        1. Add a new user button to the process page. When this button is clicked, the cookie will not be read and a new bean will be created.
        2. Remove the new user button that uses the GET method.
  2. Add CookieUtil to the shared package.
  3. Modify the web.xml file.
    1. Add a servlet definition.
    2. Add a servlet mapping.
  4. Modify index.jsp
    1. Add a link to the new controller.

Shopping Cart

  1. Add a hibernate.cfg.xml file to the classes directory. For NetBeans, place it in the source packages folder (it will appear in the default package). Add all the necessary properties to the file.
  2. Create a new package named ch7.catalog in the project.
    1. Add a controller to the package.
    2. Add a bean for a catalog item with itemId, name, description and cost properties.
      1. Validate that name is not null and is between 1 and 50 characters.
      2. Validate that itemId is not null and is between 1 and 10 characters.
      3. Set description so that it can contain an unlimited amount of text.
      4. Modify the bean so that it can be saved to the database in its own table.
    3. Add a controller helper to the package.
      1. Add a member variable for the current item.
        1. Add an accessor for it.
      2. Add a member variable for the generic shopping cart.
        1. Add an accessor for it.
      3. Initialise the session factory with the catalog item class.
      4. Copy the item and the cart from the session.
      5. Create a default method.
        1. The default method should retrieve all the items from the database.
        2. Place the list of items in the request, so the JSP can access it.
      6. Create a method to add an item to the cart.
        1. Point the item to an empty item after it is added to the cart.
      7. Create a method to clear the items from the cart.
      8. Create a method to view the details of an item.
        1. Retrieve the item id from the query data.
        2. Validate that it has the correct format.
        3. Try to retrieve the item from the database. If it is there, make it the current item.
      9. Create method that will go to the page that displays the contents of the cart.
      10. Create a method that will calculate the total cost and count of items in the cart, then it will go to a page to display the information.
    4. Add a bean for a shopping cart.
      1. Make it a generic bean that has a generic parameter for the objects that are placed in the cart.
      2. Add a property for the generic list that contains the objects in the cart.
      3. Add a reset method that removes all the objects from the list in the cart.
      4. Add a method for adding an item to the list in the cart.
      5. Add a double property for the total cost of all the items in the cart.
      6. Add a method to add a double to the total.
      7. Add a method that returns the total in the local currency.
      8. Add an integer property for the number of items in the cart.
      9. Add a method that increments the count.
    5. Add a servlet that will create the database for the catalog items.
      1. In the init method, create the database and initialise the hibernate session.
      2. Create a static, generic list of catalog items.
      3. Add a static block to initialise the catalog with at least four items.
      4. In the doGet method, add the list to the database and print a message  of completion to the browser.
    6. Add a default page.
      1. Display a button for viewing the cart.
      2. Display all the items in the catalog.
      3. For each item, display a button for viewing the details of the item.
      4. When the details are displayed, add a button for adding the item to the cart.
    7. Add a process page.
      1. Display all the items in the cart.
      2. Display the total cost of items.
      3. Display the count of items.
    8. Add a page for displaying the items in the cart.
      1. Display all the items in the cart.
      2. Add a button to continue shopping.
      3. Add a button to empty the cart.
      4. Add a button to process the cart.
  3. Modify the web.xml file.
    1. Add a servlet definition for the controller.
    2. Add a servlet mapping for the controller.
    3. Add a servlet definition for the servlet that creates the catalog database.
    4. Add a servlet mapping for the servlet that creates the catalog database.
  4. Modify index.jsp
    1. Add a link to the new controller.
    2. Add a link to the servlet that creates the database.


Contact the author