AddressBook/web/DoModify.jsp

Notes

Purpose
The purpose of DoModify.jsp is to produce a web page with a table displaying the address parameters passed from RequestModify.jsp; actually perform the database update; display whether the database was successfully updated or not; and wait until the user selects the link back to Home.jsp. Figure 16 shows an example of the DoModify web page.

[Figure16]


Outer Shell
This html code outputs the shell of the page with a link to Home.jsp at the bottom of the page:

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

            ... code to display the table goes here ...

            ... code to update the database and display success or failure goes here ...

            <hr>
            <center>
                <b><a href="Home.jsp">Back to AddressBook Home Page</a></b>
            </center>
        </body>
    </html>


Display the Table
This html code displays a table; the left column of the table displays the names of the address fields; the right column contains the new address data retrieved from the parameters:

    <table align="center" cellpadding="2" cellspacing="2" border="1" width="80%" bgcolor="#dddddd">
        <tr>
            <th>ID:</th>
            <td><%= request.getParameter ("id") %></td>
        </tr>
        <tr>
            <th>Surname:</th>
            <td><%= request.getParameter ("surname") %></td>
        </tr>
        <tr>
            <th>Firstname:</th>
            <td><%= request.getParameter ("firstname") %></td>
        </tr>
        <tr>
            <th>Street:</th>
            <td><%= request.getParameter ("street") %></td>
        </tr>
        <tr>
            <th>District:</th>
            <td><%= request.getParameter ("district") %></td>
        </tr>
        <tr>
            <th>City:</th>
            <td><%= request.getParameter ("city") %></td>
        </tr>
        <tr>
            <th>Postcode:</th>
            <td><%= request.getParameter ("postcode") %></td>
        </tr>
    </table>


Update the Database and Display Success or Failure
This java code interspersed with html 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");

It constructs an Address from the address parameters passed from RequestModify.jsp:

    AddressBook.Address address = new AddressBook.Address
    (
        null,
        request.getParameter ("surname"),
        request.getParameter ("firstname"),
        request.getParameter ("street"),
        request.getParameter ("district"),
        request.getParameter ("city"),
        request.getParameter ("postcode")
    );

Then the Address is then modified on the addresses database:

    int rowsAffected = addressesDB.modifyAddress (address);

If only one row is affected by the database update then "Successfully Modified Address" is displayed in green, if not, then "Failed to Modify Address" is displayed in red:

    if (rowsAffected == 1)
    {
%>
        <center>
            <h2><font color="#00cc00">Successfully Modified Address <%= request.getParameter ("id") %></font></h2>
        </center>
<%
    }
    else
    {
%>
        <center>
            <h2><font color="#cc0000">Failed to Modify Address <%= request.getParameter ("id") %></font></h2>
        </center>
<%
    }


Complete Source Code


<%@ page language="java" %>

<html>
    <head>
        <title>AddressBook: Modifying Address <%= request.getParameter ("id") %></title>
    </head>
    <body bgcolor="#ffffee">
        <h1><center>AddressBook: Modifying Address <%= request.getParameter ("id") %></center></h1>
        <table align="center" cellpadding="2" cellspacing="2" border="1" width="80%" bgcolor="#dddddd">
            <tr>
                <th>ID:</th>
                <td><%= request.getParameter ("id") %></td>
            </tr>
            <tr>
                <th>Surname:</th>
                <td><%= request.getParameter ("surname") %></td>
            </tr>
            <tr>
                <th>Firstname:</th>
                <td><%= request.getParameter ("firstname") %></td>
            </tr>
            <tr>
                <th>Street:</th>
                <td><%= request.getParameter ("street") %></td>
            </tr>
            <tr>
                <th>District:</th>
                <td><%= request.getParameter ("district") %></td>
            </tr>
            <tr>
                <th>City:</th>
                <td><%= request.getParameter ("city") %></td>
            </tr>
            <tr>
                <th>Postcode:</th>
                <td><%= request.getParameter ("postcode") %></td>
            </tr>
        </table>
<%
        AddressBook.AddressesDB addressesDB = (AddressBook.AddressesDB) application.getAttribute ("addressesDB");
        AddressBook.Address address = new AddressBook.Address
        (
            request.getParameter ("id"),
            request.getParameter ("surname"),
            request.getParameter ("firstname"),
            request.getParameter ("street"),
            request.getParameter ("district"),
            request.getParameter ("city"),
            request.getParameter ("postcode")
        );
        int rowsAffected = addressesDB.modifyAddress (address);
        if (rowsAffected == 1)
        {
%>
            <center>
                <h2><font color="#00cc00">Successfully Modified Address <%= request.getParameter ("id") %></font></h2>
            </center>
<%
        }
        else
        {
%>
            <center>
                <h2><font color="#cc0000">Failed to Modify Address <%= request.getParameter ("id") %></font></h2>
            </center>
<%
        }
%>
        <hr>
        <center>
            <b><a href="Home.jsp">Back to AddressBook Home Page</a></b>
        </center>
    </body>
</html>