Download the following JAR files from the book's web site.
log4j
commons collections
commons loggin
commons beanutils
Right-click the Libraries folder in the NetBeans project.
Select Add Jar/Folder
Navigate to the directory that contains the downloaded JAR files.
Select all four JAR files and select Open.
Open the Source Packages folder.
Create a new package named shared.
Create a new class named InitLog4j. Copy the contents for it
from the book's web site.
Create a new class named HelperBaseCh4.
Copy the contents of the HelperBase file from the Chapter 3
tutorials.
Change the name in all the appropriate places to HelperBaseCh4.
Create a new class named ButtonMethod. Copy the contents for
it from the book's web site.
Create a new package named ch4.enhanced.
Copy the controller class and the controller helper class from the
Reorganised tutorial for Chapter 3. Refactor as necessary.
Copy the edit, confirm and process pages from the Reorganised
tutorial for Chapter 3 into this package.
Modify HelperBaseCh4 file.
Change the class to an abstract class.
Add a member variable for a logger object of type
org.apache.log4j.Logger.
Read the initialization parameter that contains the name for the log file. Get the logger with that name.
Read the initialization parameter that contains the level for the log file. Set the level of the logger to that level.
Add an enumerated type named SessionData with the values
READ and IGNORE.
Add an abstract method named copyFromSession that has one parameter
of type Object.
Add a method named addHelperToSession that has two parameters
of type String and type SessionData.
If the session data parameter is READ, then try to read an object
with the same name as the string parameter from the session. If the object
is not null, then call copyFromSession with that object.
Save the current object into the session using the name in the string parameter
as the key.
Add a method named fillBeanFromRequest with one parameter of
type Object.
Try to call the populate method from the
org.apache.commons.beanutils.BeanUtils class. Pass the object
parameter and the parameter map from the request to the method.
Catch the possible exceptions and write an appropriate message to your error
log.
IllegalAccessException
java.lang.reflect.InvocationTargetException
Add a member variable named methodDefault of type
java.lang.reflect.Method.
Copy the following methods from the online version of
HelperBaseCh4.
Copy the executeButtonMethod method that has no parameters.
This method looks for the most derived class in a class heirarchy.
Copy the executeButtonMethod method that has two parameters.
This method searches throuh all the methods in the class, looking for the
method with an associated button name.
If no method is found in the class, then the super class is searched.
If the method is found, then it is executed.
After all super classes have been searched and no button name has been matched,
then the default method is called.
Copy the invokeButtonMethod method. This is the method that
executes the method that was found for the button name in the query string.
Copy the writeError method. This writes an error message to
the browser if the button method cannot be executed.
These methods refer to a logger named logger. If your logger
has a different name, then replace all these occurences with the name of
your logger.
Add appropriate import statements.
Modify ControllerHelper file.
Extend it from shared.HelperBaseCh4.
Implement the abstract method copyFromSession.
Test if the parameter has the same type as the current object.
If it does, then cast it to the same type and copy the data object from the
session object into the data for the current object.
Modify the doGet method.
Replace the statements that add the bean to the session and fill the bean
from the query string with a call to the addHelperToSession
method.
Choose an appropriate name. This name will be used in the JSPs to access
the controller helper.
Set the session data to READ, so that the old data will be read
from the session.
Replace the nested if block with the single statement that assigns the return
value of the executeButtonMethod to the address variable.
Add a method named jspLocation.
The method returns a string.
The method has a parameter that is a string.
The method returns the full path to the JSPs concatenated with the string
parameter. The path should be to the directory of the controller helper,
since the JSPs have been copied there.
Add a public method that returns a string for each button in the
application.
Annotate each method with the ButtonMethod annotation. Use the
correct button name for each method.
Set the method for the edit page as the default method. This corresponds
to the final else in the old nested if block.
Each method should return the result of the jspLocation method
with the name of the JSP that should be executed.
In the method that corresponds to the confirm button, call the
fillBeanFromRequest method.
Edit the web.xml file.
Add a servlet definition for the InitLog4j servlet.
Include an initialization parameter for the location of the log file. The name of the parameter should be logPath. Give it a value of /WEB-INF/logs/error.ch4.log
Add the load on startup tag.
Add a servlet definition for the controller.
Include an initialization parameter named logName for the default name of the logger. Set the name as bytesizebook.guide.ch4
Include an initialization parameter named logLevel for the default level of the logger. Set the level as INFO
Add a servlet mapping for the controller.
Do not create servlet definitions or servlet mappings for any of the other
files in the package.
Modify the JSPs. Replace all EL statements that used the bean reference so
that they now use the helper and the bean in the helper.
Replace ${refData.hobby} with ${helper.data.hobby}.
If you used a name other than helper to save the helper in the
session, then use that name instead.
Replace ${refData.aversion} with
${helper.data.aversion}. If you used a name other than
helper to save the helper in the session, then use that name
instead.
Remove all the hidden fields.
Open the Web Pages folder.
Open the WEB-INF folder.
Create folder named logs.
Open the logs folder.
Create a file named error.ch4.log.
Modify the index.jsp file.
Modify the page by adding a hypertext link to the controller.
Do not link to the edit page; all accesses should be to the controller.
Run the project.
The index.jsp page will appear in the browser.
Enter data into each field.
Access each page of the application and click each button on each page.
Notice that the data is sent from page to page.
Return to the edit page by clicking the edit button.
Click in the location bar in the browser and press enter.
You will see that the old data is still shown in the input elements, even
though there is no data in the query string. That is because the data is
being read from the session.
In your browser, remove the cookie for the JSESSIONID.
Click in the location bar in the browser and press enter.
You will see that the data has dissappeared. The JSESSIONID cookie is what
identifies the browser with the Tomcat session. When the cookie is gone,
Tomcat cannot access the session.
Click to the File tab in the NetBeans project.
Open the build folder.
Open the web folder.
Open the WEB-INF folder.
Open the logs folder.
Open the error.ch4.log folder. You should see some messages there.
Validate that all the JSP files contain valid HTML.
Whenevere a log file is used. you will need to shut down Tomcat in NetBeans
before doing a clean and build.