Download the following JAR files from the book's web site.
log4j
commons collections
commons logging
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 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 package named ch4.enhanced.
Copy the controller class and the controller helper class from the
Reorganised tutorial for Chapter 3. Refactor as necessary.
Create a visible folder named ch4/enhanced for the JSPs. Copy the JSPs from the reorganised controller from Chapter 3.
Adding a Logger
Open the Source Packages folder.
Create a new class named InitLog4j in the shared package. Copy the contents for it
from the book's web site.
Modify the HelperBaseCh4 file in the shared package.
Add a member variable for a logger object of type
org.apache.log4j.Logger.
Create a helper method named initLogger.
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.
Call the initLogger method from the helper base constructor.
Modify ControllerHelper file.
Extend it from shared.HelperBaseCh4.
Add an info message to the following methods. Write a message like "Called method-name method" for each:
ControllerHelper constructor.
ControllerHelper doGet.
HelperBase constructor.
HelperBase initLogger.
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.
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.
Click the Services tab in the NetBeans project.
Open the Servers folder.
Right-click Apache Tomcat.
Click Stop.
Now you can do a clean and build.
Eliminating Hidden Fields
The copyFromSession and addHelperToSession methods are the keys to removing the hidden fields. The servlet can access data that was entered in the session during a previous request.
Modify HelperBaseCh4 file.
Change the class to an abstract class.
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.
Modify ControllerHelper file.
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 statement that adds the helper to the session
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.
The lines that copy the query string data into the bean must be moved. Move them into the if .. else block for the confirm button.
Modify the JSPs.
Remove all the hidden fields.
Run the project.
Specifying the Location of JSPs
The jspLocation is used to define the path to the JSPs. It will be used to move the JSPs into the source package that contains the controller.
Modify ControllerHelper file.
Move the JSPs form the visible folder ch4/enhanced into the source package that contains the controller.
Modify the doGet method.
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 statement after the if .. else block, but before the request dispatcher statement, that modifies the address by calling the jspLocation method.
Automating the Controller Logic
The if .. else block of code can be removed from the controller. It will be replaced with a loop in the helper base. Methods will be used to handle each button that the user clicks.
Add a class named ButtonMethod to the shared package. Copy the contents from the book's web site.
Modify HelperBaseCh4 file.
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 through 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.
Modify the doGet method.
Replace the nested if block with the single statement that assigns the return
value of the executeButtonMethod to the address variable.
Modify the address by calling jspLocation.
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.
In the method that corresponds to the confirm button, add the code that copies the query string data into the bean.
Automating Filling the Bean
Define the fillBeanFromRequest method and use it to automatically copy data from the query string into the bean.
Modify HelperBaseCh4 file.
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
Modify the confirm method in the ControllerHelper.
Remove the statements that copy the query string into the bean.