Chapter 3 Tutorials

Chapter 3 Tutorials

Start Example

  1. Use the NetBeans project that was created in the Chapter 1 Tutorials.
  2. Open the Source Packages folder.
    1. Create a new package named ch3.startExample.
    2. Copy the controller class from the servlet controller tutorial for Chapter 2.
    3. You will be prompted to refactor the file. Accept the refactor. This will copy the file to the current package and modify the package statement accordingly.
  3. Open the Configuration File folder.
    1. Right-click the web.xml file.
    2. Select Edit.
    3. Add a servlet definition for the controller.
      1. The servlet class is a combination of the package and the name of the class.
      2. The servlet class is ch3.startExample.Controller.
    4. Add a servlet mapping for the controller.
      1. Use the same name as was used in the servlet definition.
      2. Use the URL pattern /ch3/startExample/Controller.
      3. The leading / in the pattern refers to the root of the web application, not the root of the server.
        1. The complete path from the root of the server would have to include the name of the web application.
        2. Do not include the name of the web application in the URL pattern.
  4. Open the Web Pages folder.
    1. Create a new folder named ch3.
    2. Create a subfolder of ch3 named startExample. This is the folder that corresponds to the URL pattern for the controller. By placing the JSPs in this directory, the controller can use a relative reference to the JSPs.
    3. Copy the edit, confirm and process pages from the servlet controller tutorial for Chapter 2.
    4. Modify the edit page.
      1. Add a second text element to the edit page.
      2. Initialize the element with the corresponding data from the query string.
    5. Modify the confirm page.
      1. Echo the values of both text elements that are sent in the query string.
      2. Add a second hidden field for the new text element.
        1. Initalize the new hidden field with the corresponding data from the query string.
        2. The name for the new hidden field should be the same as the name of the new text element in the edit page.
    6. Modify the process page.
      1. Add a form that sends its data to the controller.
      2. Add hidden fields for both text elements.
      3. Add a button for the edit page.
  5. Modify the index.jsp file already exists in the Web Pages folder.
    1. This is the welcome page for the application.
    2. Modify the page by adding a hypertext link to the controller.
      1. Use the value from the URL pattern in the web.xml file, but remove the leading /.
      2. The leading / can be removed, since the index.jsp file is in the root of the web application and the leading slash in the URL pattern refers to the root of the web application.
    3. Do not link to the edit page; all accesses should be to the controller.
  6. 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.
  7. Validate that all the JSP files contain valid HTML.

Data Bean

  1. Open the Source Packages folder.
    1. Create a new package named ch3.dataBean.
    2. Add a class named RequestData.
      1. Add a bean property for each of the text fields from the StartExample tutorial.
      2. Be sure that the names of the accessors and mutators have the correct case and spelling.
    3. Copy the controller from the StartExample tutorial.
      1. You will be prompted to refactor the file. Accept the refactor. This will copy the file to the current package and modify the package statement accordingly. Remove the import for ch3.startExample.*
      2. Insert the following at the start of the doGet method.
        1. Declare and instantiate an object of type RequestData.
        2. Add the bean object to the session. Use a name of your choice for the key to the session object.
        3. Copy the values from the query string into the corresponding properties in the bean object.
  2. Open the Configuration File folder.
    1. Right-click the web.xml file.
    2. Select Edit.
    3. Add a servlet definition for the controller.
      1. The servlet class is a combination of the package and the name of the class.
      2. The servlet class is ch3.dataBean.Controller.
    4. Add a servlet mapping for the controller.
      1. Use the same name as was used in the servlet definition.
      2. Use the URL pattern /ch3/dataBean/Controller.
  3. Open the Web Pages folder.
    1. Create a subfolder of ch3 named dataBean. This is the folder that corresponds to the URL pattern for the controller.
    2. Copy the edit, confirm and process pages from the StartExample tutorial.
    3. Modify all of the EL statements in all the JSPs.
      1. Replace all occurences of param with the name that you used for the key to the session object.
      2. From now on, all references to the data will be through the session, not through the param object.
  4. Modify the index.jsp file already exists in the Web Pages folder.
    1. This is the welcome page for the application.
    2. Modify the page by adding a hypertext link to the controller.
      1. Use the value from the URL pattern in the web.xml file, but remove the leading /.
      2. The leading / can be removed, since the index.jsp file is in the root of the web application and the leading slash in the URL pattern refers to the root of the web application.
    3. Do not link to the edit page; all accesses should be to the controller.
  5. 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. The JSPs are now reading the data from the bean, not the query string.
  6. Validate that all the JSP files contain valid HTML.

Default Validation

  1. Open the Source Packages folder.
    1. Create a new package named ch3.defaultValidate.
    2. Add a class named RequestDataDefault.
      1. Copy the contents of RequestData from the DataBean tutorial.
      2. Add default validation for both properties.
        1. Write a helper method for each that tests if the data is valid.
        2. In the acceessor of each property, return a default value if the data is not valid.
    3. Copy the controller from the DataBean tutorial and modify the doGet emthod.
      1. Change the name of the bean class to RequestDataDefault.
      2. This application will use the same JSPs from the DataBean tutorial.
      3. Instead of setting the address to just the name of the page, include the path to each JSP.
        1. "/ch3/dataBean/Edit.jsp"
        2. "/ch3/dataBean/Confirm.jsp"
        3. "/ch3/dataBean/Process.jsp"
  2. Open the Configuration File folder.
    1. Right-click the web.xml file.
    2. Select Edit.
    3. Add a servlet definition for the controller.
      1. The servlet class is a combination of the package and the name of the class.
      2. The servlet class is ch3.defaultValidate.Controller.
    4. Add a servlet mapping for the controller.
      1. Use the same name as was used in the servlet definition.
      2. Use the URL pattern /ch3/defaultValidate/Controller.
      3. Since the controller includes the path to the JSPs when calculating the address of the next page, the URL pattern does not have to match the location of the JSPs.
  3. Modify the index.jsp file already exists in the Web Pages folder.
    1. This is the welcome page for the application.
    2. Modify the page by adding a hypertext link to the controller.
      1. Use the value from the URL pattern in the web.xml file, but remove the leading /.
      2. The leading / can be removed, since the index.jsp file is in the root of the web application and the leading slash in the URL pattern refers to the root of the web application.
    3. Do not link to the edit page; all accesses should be to the controller.
  4. Run the project.
    1. The index.jsp page will appear in the browser.
    2. Default values appear in the input elements.
    3. Clear the fields and click the confirm button.
    4. Default values will appear on the confirm page.
    5. Return to the edit page and change the values in the input elements.
    6. Click the confirm button.
    7. The default values are not used.
    8. Click the edit button.
    9. The default values are not used.
    10. As long as the user enters some data, the default values will not appear.
  5. Validate that all the JSP files contain valid HTML.

Shared Error

  1. Open the Source Packages folder.
    1. Create a new package named ch3.sharedVariable.error.
    2. Create a controller in this package.
      1. Copy the contents of the ch3.shared.variable.error controller from the book's web site.
      2. This controller will use a JSP from a different application, so it uses the path to the JSP.
      3. There is a helper method that simulates how a computer increments a variable.
        1. The simulation is much slower than the computer.
        2. Notice that there is a member variable in the controller that is incremented during each GET request.
        3. Notice that the member variable has been added to the session with the name accessCount.
  2. Open the Configuration File folder.
    1. Right-click the web.xml file.
    2. Select Edit.
    3. Add a servlet definition for the controller.
      1. The servlet class is a combination of the package and the name of the class.
      2. The servlet class is ch3.sharedVariable.error.Controller.
    4. Add a servlet mapping for the controller.
      1. Use the same name as was used in the servlet definition.
      2. Use the URL pattern /ch3/sharedVariable/error/Controller.
      3. Since the controller includes the path to the JSPs when calculating the address of the next page, the URL pattern does not have to match the location of the JSPs.
  3. Open the Web Pages folder.
    1. Create a subfolder of ch3 named sharedVariable.
    2. Create an Edit.jsp file that echoes the value of the accessCount from the session. Use EL.
    3. This JSP is in the sharedVariable folder because it will be used by the  controller in the next tutorial.
  4. Modify the index.jsp file already exists in the Web Pages folder.
    1. This is the welcome page for the application.
    2. Modify the page by adding a hypertext link to the controller.
      1. Use the value from the URL pattern in the web.xml file, but remove the leading /.
      2. The leading / can be removed, since the index.jsp file is in the root of the web application and the leading slash in the URL pattern refers to the root of the web application.
    3. Do not link to the edit page; all accesses should be to the controller.
  5. Run the project.
    1. The index.jsp page will appear in the default browser.
    2. Open the URL in a different browser, not just another browser window. If Firefox is your default browser, then open IE (and vice versa).
    3. Reload the page in Firefox.
    4. Within three seconds (as quickly as possible), reload the page in IE.
    5. You will see that both browsers show the same value for the incremented variable.
    6. Reload the page in Firefox.
    7. Reload the page in IE after waiting 3 seconds.
    8. You will see that the incremented value is different in each browser.

Shared Variable

  1. Open the Source Packages folder in the NetBeans project. Right-click Source Packages.
    1. Select New.
    2. Select Java Class.
    3. Enter the name as Controller.
    4. Enter the package as ch3.sharedVariable.
    5. Extend the class from ch3.sharedVariable.error.Controller.
      1. Override the incrementSharedVariable from that controller.
      2. Call the super class's method from within a synchronized block.
  2. Open the Configuration File folder.
    1. Right-click the web.xml file.
    2. Select Edit.
    3. Add a servlet definition for the controller.
      1. The servlet class is a combination of the package and the name of the class.
      2. The servlet class is ch3.sharedVariable.Controller.
    4. Add a servlet mapping for the controller.
      1. Use the same name as was used in the servlet definition.
      2. Use the URL pattern /ch3/sharedVariable/Controller.
  3. Modify the index.jsp file already exists in the Web Pages folder.
    1. This is the welcome page for the application.
    2. Modify the page by adding a hypertext link to the controller.
      1. Use the value from the URL pattern in the web.xml file, but remove the leading /.
      2. The leading / can be removed, since the index.jsp file is in the root of the web application and the leading slash in the URL pattern refers to the root of the web application.
    3. Do not link to the edit page; all accesses should be to the controller.
  4. Run the project.
    1. The index.jsp page will appear in the browser.
    2. Follow the same steps as for the Shared Error tutorial.
    3. Notice that the browsers always get a new number, but the second request must wait for the first one to finish before it starts.

Reorganised

  1. Create a new package named ch3.reorganised.
  2. Add a new Java class to this package. Name it HelperBase.
    1. Add a protected member variable for a HttpServlet object.
    2. Add a protected member variable for a HttpServletRequest object.
    3. Add a protected member variable for a HttpServletResponse object.
    4. Add a constructor that has three parameters for the HttpServlet, HttpServletRequest and HttpServletResponse.
    5. In the constructor, initialize the member variables from the parameters.
    6. Do not add a default constructor.
  3. Add a new Java class to this package. Name it ControllerHelper.
    1. Extend it from the HelperBase class.
    2. Do not add a default constructor.
    3. Create a constructor that has three parameters.
      1. The parameters should be of type HttpServlet, HttpServletRequest and HttpServletResponse
      2. Call the super class's constructor with the servlet, request and response parameters.
    4. This application will reuse the bean from the defaultValidate tutorial.
      1. Add an import for that class.
      2. Add a protected member variable for the bean and instantiate it.
    5. Add a protected member variable for the bean.
    6. Add a public method named getData that returns the bean as an Object.
    7. Add a public doGet method that has no parameters and throws IOException and ServletException.
      1. Add the current controller helper object to the session, using the string "helper".
      2. Fill the properties in the bean from the parameters in the query string.
      3. Decode the address, like previous controllers did.
      4. Forward the request and response to the decoded address, like previous controllers did.
  4. Create a controller in this package.
    1. Extend the controller from HttpServlet.
    2. Override the doGet method from HttpServlet.
    3. In the doGet method, only do the following.
      1. Create an object of type ControllerHelper.
      2. Call the doGet method in the ControllerHelper.
  5. Open the Configuration File folder.
    1. Right-click the web.xml file.
    2. Select Edit.
    3. Add a servlet definition for the controller.
      1. The servlet class is a combination of the package and the name of the class.
      2. The servlet class is ch3.reorganised.Controller.
    4. Add a servlet mapping for the controller.
      1. Use the same name as was used in the servlet definition.
      2. Use the URL pattern /ch3/reorganised/Controller.
  6. Open the Web Pages folder. Create a subfolder of ch3 named reorganised. This is the folder that corresponds to the URL pattern for the controller. By placing the JSPs in this directory, the controller can use a relative reference to the JSPs.
    1. Copy the edit, confirm and process pages from the dataBean tutorial.
    2. Replace all EL references to the data in the bean with the EL reference that uses the helper and the bean.
      1. Replace ${refData.hobby} with ${helper.data.hobby}
      2. Replace ${refData.aversion} with ${helper.data.aversion}
      3. From now on, all references to the data will be through the helper and the bean.
  7. Modify the index.jsp file already exists in the Web Pages folder.
    1. This is the welcome page for the application.
    2. Modify the page by adding a hypertext link to the controller.
      1. Use the value from the URL pattern in the web.xml file, but remove the leading /.
      2. The leading / can be removed, since the index.jsp file is in the root of the web application and the leading slash in the URL pattern refers to the root of the web application.
    3. Do not link to the edit page; all accesses should be to the controller.
  8. Run the project.
    1. The index.jsp page will appear in the browser.
    2. Follow the same steps as the defaultValidate tutorial.
    3. Nothing has change, except how the controller works in the background.


Contact the author