AddressBook/web/RequestModify.jsp

Notes

Purpose
The purpose of RequestModify.jsp is to produce a web page with a form showing the address data that is to be modified. The user may then either modify the data in the form and submit the form's data for processing by transferring control to DoModify.jsp or abort the process by selecting a link back to Home.jsp. Figure 15 shows an example of the RequestModify web page.

[Figure15]


Outer Shell
This html code outputs the shell of the page with a link to Home.jsp labelled "Return to AddressBook Home Page [the address will not be modified]" at the bottom of the page:

<html>
    <head>
        <title>AddressBook: Request Modify Address <%= request.getParameter ("id") %></title>
    </head>
    <body bgcolor="#ffffee">
        <h1><center>AddressBook: Request Modify Address <%= request.getParameter ("id") %></center></h1>

        ... code to retrieve address data to be modified goes here ...

        ... code to display the form goes here ...

        <hr>
        <center>
            <b><a href="Home.jsp">Return to AddressBook Home Page [the address will not be modified]</a></b>
        </center>
    </body>
</html>


Retrieve Address Data to be Modified
This java code retrieves the AddressBook.AddressesDB instance stored in the the web application's "addressesDB" attribute by AddressBook.ContextListener:

    AddressBook.AddressesDB addressesDB = (AddressBook.AddressesDB) application.getAttribute ("addressesDB");

Then it gets the address from the addresses database whose id has been passed as a parameter to this page by Home.jsp:

    AddressBook.Address address = (AddressBook.Address) addressesDB.getAddress (request.getParameter ("id"));


Display the Form
This html code with interspersed java code creates a form whose action to post the address data in the input fields to DoModify.jsp:

    <form method="post" action="DoModify.jsp">

One of these fields is a hidden field containing the id of the address to be modified - this will ensure that the id parameter passed from Home.jsp is passed on to DoModify.jsp:

    <input name="id" type="hidden" value="<%= address.getId () %>">

Then a table is displayed; the left column of the table displays the names of the address fields; the right column contains the input fields prefilled with the existing address data:

    <table align="center" cellpadding="2" cellspacing="2" border="1" width="80%" bgcolor="#dddddd">
        <tr>
            <th>Surname:</th>
            <td><input name="surname" type="text" value="<%= address.getSurname () %>"></td>
        </tr>
        <tr>
            <th>Firstname:</th>
            <td><input name="firstname" type="text" value="<%= address.getFirstname () %>"></td>
        </tr>
        <tr>
            <th>Street:</th>
            <td><input name="street" type="text" value="<%= address.getStreet () %>"></td>
        </tr>
        <tr>
            <th>District:</th>
            <td><input name="district" type="text" value="<%= address.getDistrict () %>"></td>
        </tr>
        <tr>
            <th>City:</th>
            <td><input name="city" type="text" value="<%= address.getCity () %>"></td>
        </tr>
        <tr>
            <th>Postcode:</th>
            <td><input name="postcode" type="text" value="<%= address.getPostcode () %>"></td>
        </tr>
    </table>

Then a hidden field that will simulate the pressing of the submit button when the return key is pressed is defined:

    <input name="pagemode" type="hidden" value="submit">

Then the form submit button labelled "Continue Modify Address Request nnn" is displayed:

    <input align="center" type="submit" value="Continue Modify Address Request <%= request.getParameter ("id") %>">

When the submit button is pressed the id, surname, firstname, street, district, city, and postcode parameters are passed to DoModify.jsp.


Complete Source Code


<%@ page language="java" %>

<html>
    <head>
        <title>AddressBook: Request Modify Address <%= request.getParameter ("id") %></title>
    </head>
    <body bgcolor="#ffffee">
        <h1><center>AddressBook: Request Modify Address <%= request.getParameter ("id") %></center></h1>
<%
        AddressBook.AddressesDB addressesDB = (AddressBook.AddressesDB) application.getAttribute ("addressesDB");
        AddressBook.Address address = (AddressBook.Address) addressesDB.getAddress (request.getParameter ("id"));
        if (address != null)
        {
%>
            <form method="post" action="DoModify.jsp">
            <input name="id" type="hidden" value="<%= address.getId () %>">
            <table align="center" cellpadding="2" cellspacing="2" border="1" width="80%" bgcolor="#dddddd">
                <tr>
                    <th>Surname:</th>
                    <td><input name="surname" type="text" value="<%= address.getSurname () %>"></td>
                </tr>
                <tr>
                    <th>Firstname:</th>
                    <td><input name="firstname" type="text" value="<%= address.getFirstname () %>"></td>
                </tr>
                <tr>
                    <th>Street:</th>
                    <td><input name="street" type="text" value="<%= address.getStreet () %>"></td>
                </tr>
                <tr>
                    <th>District:</th>
                    <td><input name="district" type="text" value="<%= address.getDistrict () %>"></td>
                </tr>
                <tr>
                    <th>City:</th>
                    <td><input name="city" type="text" value="<%= address.getCity () %>"></td>
                </tr>
                <tr>
                    <th>Postcode:</th>
                    <td><input name="postcode" type="text" value="<%= address.getPostcode () %>"></td>
                </tr>
            </table>
            <br>
            <center>
                <input name="pagemode" type="hidden" value="submit">
                <input align="center" type="submit" value="Continue Modify Address Request <%= request.getParameter ("id") %>">
            </center>
<%
        }
        else
        {
%>
            <center>
                <h2><font color="#cc0000">Could Not Find Address <%= request.getParameter ("id") %></font></h2>
            </center>
<%
        }
%>
        <hr>
        <center>
            <b><a href="Home.jsp">Return to AddressBook Home Page [the address will not be modified]</a></b>
        </center>
    </body>
</html>