Chapter 4 Tutorials

Chapter 4 Tutorials

Enhanced

  1. Use the NetBeans project that was created in the Chapter 1 Tutorials.
  2. Download the following JAR files from the book's web site.
    1. log4j
    2. commons collections
    3. commons loggin
    4. commons beanutils
  3. Right-click the Libraries folder in the NetBeans project.
    1. Select Add Jar/Folder
    2. Navigate to the directory that contains the downloaded JAR files.
    3. Select all four JAR files and select Open.
  4. Open the Source Packages folder.
    1. Create a new package named shared.
      1. Create a new class named InitLog4j. Copy the contents for it from the book's web site.
      2. Create a new class named HelperBaseCh4.
        1. Copy the contents of the HelperBase file from the Chapter 3 tutorials.
        2. Change the name in all the appropriate places to HelperBaseCh4.
      3. Create a new class named ButtonMethod. Copy the contents for it from the book's web site.
    2. Create a new package named ch4.enhanced.
      1. Copy the controller class and the controller helper class from the Reorganised tutorial for Chapter 3. Refactor as necessary.
      2. Copy the edit, confirm and process pages from the Reorganised tutorial for Chapter 3 into this package.
  5. Modify HelperBaseCh4 file.
    1. Change the class to an abstract class.
    2. Add a member variable for a logger object of type org.apache.log4j.Logger.
      1. Read the initialization parameter that contains the name for the log file. Get the logger with that name.
      2. Read the initialization parameter that contains the level for the log file. Set the level of the logger to that level.
    3. Add an enumerated type named SessionData with the values READ and IGNORE.
    4. Add an abstract method named copyFromSession that has one parameter of type Object.
    5. Add a method named addHelperToSession that has two parameters of type String and type SessionData.
      1. 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.
      2. Save the current object into the session using the name in the string parameter as the key.
    6. Add a method named fillBeanFromRequest with one parameter of type Object.
      1. 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.
      2. Catch the possible exceptions and write an appropriate message to your error log.
        1. IllegalAccessException
        2. java.lang.reflect.InvocationTargetException
    7. Add a member variable named methodDefault of type java.lang.reflect.Method.
    8. Copy the following methods from the online version of HelperBaseCh4.
      1. Copy the executeButtonMethod method that has no parameters. This method looks for the most derived class in a class heirarchy.
      2. Copy the executeButtonMethod method that has two parameters.
        1. This method searches throuh all the methods in the class, looking for the method with an associated button name.
        2. If no method is found in the class, then the super class is searched.
        3. If the method is found, then it is executed.
        4. After all super classes have been searched and no button name has been matched, then the default method is called.
      3. Copy the invokeButtonMethod method. This is the method that executes the method that was found for the button name in the query string.
      4. Copy the writeError method. This writes an error message to the browser if the button method cannot be executed.
      5. 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.
      6. Add appropriate import statements.
  6. Modify ControllerHelper file.
    1. Extend it from shared.HelperBaseCh4.
    2. Implement the abstract method copyFromSession.
      1. Test if the parameter has the same type as the current object.
      2. 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.
    3. Modify the doGet method.
      1. 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.
        1. Choose an appropriate name. This name will be used in the JSPs to access the controller helper.
        2. Set the session data to READ, so that the old data will be read from the session.
      2. Replace the nested if block with the single statement that assigns the return value of the executeButtonMethod to the address variable.
    4. Add a method named jspLocation.
      1. The method returns a string.
      2. The method has a parameter that is a string.
      3. 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.
    5. Add a public method that returns a string for each button in the application.
      1. Annotate each method with the ButtonMethod annotation. Use the correct button name for each method.
      2. Set the method for the edit page as the default method. This corresponds to the final else in the old nested if block.
      3. Each method should return the result of the jspLocation method with the name of the JSP that should be executed.
      4. In the method that corresponds to the confirm button, call the fillBeanFromRequest method.
  7. Edit the web.xml file.
    1. 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.
    2. 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
    3. Add a servlet mapping for the controller.
    4. Do not create servlet definitions or servlet mappings for any of the other files in the package.
  8. 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.
    1. 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.
    2. 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.
    3. Remove all the hidden fields.
  9. Open the Web Pages folder.
    1. Open the WEB-INF folder.
    2. Create folder named logs.
    3. Open the logs folder.
    4. Create a file named error.ch4.log.
  10. Modify the index.jsp file.
    1. Modify the page by adding a hypertext link to the controller.
    2. Do not link to the edit page; all accesses should be to the controller.
  11. Run the project.
    1. The index.jsp page will appear in the browser.
    2. Enter data into each field.
    3. Access each page of the application and click each button on each page.
    4. Notice that the data is sent from page to page.
    5. Return to the edit page by clicking the edit button.
    6. Click in the location bar in the browser and press enter.
    7. 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.
    8. In your browser, remove the cookie for the JSESSIONID.
    9. Click in the location bar in the browser and press enter.
    10. 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.
    11. Click to the File tab in the NetBeans project.
      1. Open the build folder.
      2. Open the web folder.
      3. Open the WEB-INF folder.
      4. Open the logs folder.
      5. Open the error.ch4.log folder. You should see some messages there.
  12. Validate that all the JSP files contain valid HTML.
  13. Whenevere a log file is used. you will need to shut down Tomcat in NetBeans before doing a clean and build.
    1. Click the Services tab in the NetBeans project.
    2. Open the Servers folder.
    3. Right-click Apache Tomcat.
    4. Click Stop.
    5. Now you can do a clean and build.

Contact the author