Chapter 7 Tutorials
Account Login
-
Create a new package named
ch7.accountLogin
in the
book
web application.
-
Copy all the files from
ch5.persistentData
into the new
package.
-
Rename the bean to
RequestDataAccount.java
-
Refactor all the files that reference the bean.
-
Modify the bean.
-
Add an account number property.
-
Validate that the account number is not null and starts with two letters
and is followed by three digits.
-
The account number should have a column in the database.
-
Create the
Login.jsp
file.
-
Add a form.
-
Add an input element for the acount number.
-
Initialise the account number with the data from the bean.
-
Add a button for logging in.
-
Modify the controller helper.
-
Create a login method.
-
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.
-
If the account number has the correct format, do the following
-
Clear the errors.
-
Try to read the account number from the database.
-
If there is data, set it to the bean in the controller helper.
-
The next page should be the edit page.
-
If the account is not valid
-
The next page should be the login page.
-
Display an error for the account number.
-
Make this the default method (remove default from the edit method).
-
Modify the
doGet
method so that the default method is the login
method.
-
Modify the
jspLocation
method with the path to the JSPs.
-
Modify
web.xml
.
-
Add an init parameter named create to the servlet definition.
-
Add a servlet mapping.
-
Modify
index.jsp
-
Add a link to the new controller.
Account Removal
-
Create a new package named
ch7.accountRemoval
in the
book
web application.
-
Modify the controller helper from the account login package. Do not make
a copy of it, modify the actual controller helper.
-
Be sure that the
doGet
method is public.
-
Be sure that the
doPost
method is public.
-
Create a new controller helper that extends the account login controller
helper. Only three methods need to be defined.
-
Add an import for the account login bean.
-
Create a constructor that calls the constructor of the super class.
-
Override the
jspLocation
method; set the path to the JSPs.
-
Create a new method for removing the bean from the database.
-
Remove the bean from the database.
-
Make the bean point to a new bean.
-
Go to the login page.
-
Copy the controller from the account login package.
-
Do not copy the bean.
-
Copy the JSPs from the account login package.
-
Modify the process page so that it has a new button for removing the bean
from the database.
-
Modify the
web.xml
file.
-
Add a servlet definition.
-
Add a servlet mapping.
-
Modify
index.jsp
-
Add a link to the new controller.
Account Cookie
-
Create a new package named
ch7.accountCookie
in the
book
web application.
-
Modify the controller helper from the account login package. Do not make
a copy of it, modify the actual controller helper.
-
Be sure that the
doGet
method is public.
-
Be sure that the
doPost
method is public.
-
Create a new controller helper that extends the account login controller
helper.
-
Add an import for the account login bean.
-
Create a constructor that calls the constructor of the super class.
-
Override the
jspLocation
method; set the path to the JSPs.
-
Create a default method.
-
If there is not a cookie named "account", then go to the login page.
-
If there is a cookie named "account", then
-
Use the value of the cookie to access the database.
-
If there is data for the account number, then use the bean as the bean for
the controller helper.
-
Go to the edit page.
-
Override the
doGet
method. It does the same as the super class,
except it always goes to the default method.
-
Create a new user method.
-
Set the bean to a new bean.
-
Go to the login page.
-
Create a process method.
-
This will do the same as the super class process method, except that it will
also add a bean named "account" to the response.
-
The value of the bean will be the account number.
-
Modify the JSPs.
-
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.
-
Modify the process page.
-
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.
-
Remove the new user button that uses the
GET
method.
-
Add
CookieUtil
to the shared package.
-
Modify the
web.xml
file.
-
Add a servlet definition.
-
Add a servlet mapping.
-
Modify
index.jsp
-
Add a link to the new controller.
Shopping Cart
-
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.
-
Create a new package named
ch7.catalog
in the book
web application.
-
Add a controller to the package.
-
Add a bean for a catalog item with itemId, name, description and cost properties.
-
Validate that name is not null and is between 1 and 50 characters.
-
Validate that itemId is not null and is between 1 and 10 characters.
-
Set description so that it can contain an unlimited amount of text.
-
Modify the bean so that it can be saved to the database in its own table.
-
Add a controller helper to the package.
-
Add a member variable for the current item.
-
Add a member variable for the generic shopping cart.
-
Initialise the session factory with the catalog item class.
-
Copy the item and the cart from the session.
-
Create a default method.
-
The default method should retrieve all the items from the database.
-
Place the list of items in the request, so the JSP can access it.
-
Create a method to add an item to the cart.
-
Point the item to an empty item after it is added to the cart.
-
Create a method to clear the items from the cart.
-
Create a method to view the details of an item.
-
Retrieve the item id from the query data.
-
Validate that it has the correct format.
-
Try to retrieve the item from the database. If it is there, make it the current
item.
-
Create method that will go to the page that displays the contents of the
cart.
-
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.
-
Add a bean for a shopping cart.
-
Make it a generic bean that has a generic parameter for the objects that
are placed in the cart.
-
Add a property for the generic list that contains the objects in the cart.
-
Add a reset method that removes all the objects from the list in the cart.
-
Add a method for adding an item to the list in the cart.
-
Add a double property for the total cost of all the items in the cart.
-
Add a method to add a double to the total.
-
Add a method that returns the total in the local currency.
-
Add an integer property for the number of items in the cart.
-
Add a method that increments the count.
-
Add a servlet that will create the database for the catalog items.
-
In the init method, create the database and initialise the hibernate session.
-
Create a static, generic list of catalog items.
-
Add a static block to initialise the catalog with at least four items.
-
In the
doGet
method, add the list to the database and print
a message of completion to the browser.
-
Add a default page.
-
Display a button for viewing the cart.
-
Display all the items in the catalog.
-
For each item, display a button for viewing the details of the item.
-
When the details are displayed, add a button for adding the item to the cart.
-
Add a process page.
-
Display all the items in the cart.
-
Display the total cost of items.
-
Display the count of items.
-
Add a page for displaying the items in the cart.
-
Display all the items in the cart.
-
Add a button to continue shopping.
-
Add a button to empty the cart.
-
Add a button to process the cart.
-
Modify the
web.xml
file.
-
Add a servlet definition for the controller.
-
Add a servlet mapping for the controller.
-
Add a servlet definition for the servlet that creates the catalog database.
-
Add a servlet mapping for the servlet that creates the catalog database.
-
Modify
index.jsp
-
Add a link to the new controller.
-
Add a link to the servlet that creates the database.