ch7.accountCookie
Class ControllerHelper

java.lang.Object
  extended by shared.HelperBaseCh4
      extended by shared.HelperBaseCh5
          extended by shared.HelperBaseCh6
              extended by ch7.accountLogin.ControllerHelper
                  extended by ch7.accountCookie.ControllerHelper

public class ControllerHelper
extends ControllerHelper

Write the cookie when new data is saved; read the cookie when a new request is started.

The cookie will be read in the method that is called when a Get request is made. This is considered a new request, so only read the cookie at that time.


Nested Class Summary
 
Nested classes/interfaces inherited from class shared.HelperBaseCh4
HelperBaseCh4.SessionData
 
Field Summary
 
Fields inherited from class ch7.accountLogin.ControllerHelper
data
 
Fields inherited from class shared.HelperBaseCh6
checked, selected
 
Fields inherited from class shared.HelperBaseCh4
logger, request, response
 
Constructor Summary
ControllerHelper(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Call the base class constructor to store the request and response.
 
Method Summary
protected  void doGet()
          GET is considered a new request, so the old data is not read and the JSP is always for the edit page.
protected  void doPost()
          Call the super class doPost method.
 java.lang.String getMethod()
          The default method.
 java.lang.String jspLocation(java.lang.String page)
          Encapsulates the location of the JSPs.
 java.lang.String newUserMethod()
          If new user is cliked, then create a new bean and go to the login page.
 java.lang.String processMethod()
          The method to call when the process button is clicked.
 
Methods inherited from class ch7.accountLogin.ControllerHelper
confirmMethod, copyFromSession, editMethod, getData, initHibernate, loginMethod
 
Methods inherited from class shared.HelperBaseCh6
addChecked, addChoice, addSelected, clearMaps, clearProperty, getChecked, getSelected, invokeGetter, setCheckedAndSelected, setCheckedAndSelected
 
Methods inherited from class shared.HelperBaseCh5
getErrors, isValid, isValidProperty, setErrors
 
Methods inherited from class shared.HelperBaseCh4
addHelperToSession, addHelperToSession, executeButtonMethod, executeButtonMethod, fillBeanFromRequest, invokeButtonMethod, writeError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ControllerHelper

public ControllerHelper(javax.servlet.http.HttpServletRequest request,
                        javax.servlet.http.HttpServletResponse response)
Call the base class constructor to store the request and response.

Parameters:
request - Request information encapsulated in a Java object
response - Response information encapsulated in a Java object
Method Detail

jspLocation

public java.lang.String jspLocation(java.lang.String page)
Encapsulates the location of the JSPs.

Overrides:
jspLocation in class ControllerHelper
Parameters:
page - The file name of the JSP.
Returns:
The full path to the JSP.

getMethod

public java.lang.String getMethod()
The default method. It is not associated with any button.

Look for a cookie named "account". If it exists, then look for an account with the value of the cookie in the database. If it is there, then set the database bean as the current data and go to the edit page; otherwise, go to the login page.

Overrides:
getMethod in class ControllerHelper
Returns:
The address for the JSP.

newUserMethod

public java.lang.String newUserMethod()
If new user is cliked, then create a new bean and go to the login page.

Overrides:
newUserMethod in class ControllerHelper
Returns:
The address of the JSP.

processMethod

public java.lang.String processMethod()
The method to call when the process button is clicked.

Test if the data is still valid; if it isn't, then the session has expired. If the data is valid, then save it to the database

Write the account number to a cookie named "account".

Retrieve all the data from the database and add it to the request so that the JSPs can access it.

The method is adorned with a ButtonMethod annotation that associates the name of a button with the method.

Overrides:
processMethod in class ControllerHelper
Returns:
The address for the JSP.

doGet

protected void doGet()
              throws javax.servlet.ServletException,
                     java.io.IOException
GET is considered a new request, so the old data is not read and the JSP is always for the edit page.
  1. Add the helper to the session but ignore the data from the old helper in the session
  2. Always call the default method.
  3. Forward the request and response to the next address.

Overrides:
doGet in class ControllerHelper
Throws:
javax.servlet.ServletException
java.io.IOException

doPost

protected void doPost()
               throws javax.servlet.ServletException,
                      java.io.IOException
Call the super class doPost method.

POST is considered a continuation of a previous request, so read the old data from the session and calculate the next page based on the button.

  1. Add the helper to the session and copy the old helper data into the current helper. This eliminates the need for hidden fields.
  2. Call the appropriate method for the clicked button. This is done from a method in the helper base that loops throuh all the button methods, looking for the one for the clicked button. This eliminates the need for the nested if-else block.
  3. Forward the request and response to the next address.

Overrides:
doPost in class ControllerHelper
Throws:
javax.servlet.ServletException
java.io.IOException