AddressBook/web/DoAdd.jsp

Notes

Purpose
The purpose of DoAdd.jsp is to produce a web page with a table displaying the address parameters passed from RequestAdd.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 12 shows an example of the DoAdd web page.

[Figure12]


The purpose of DoAdd.jsp is to display the parameters passed to it in a table; contruct an address from those parameters and add that address to the database; and display whether the database update succeeded or failed.

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: Adding Address</title>
        </head>
        <body bgcolor="#ffffee">
            <h1><center>AddressBook: Adding Address</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 with interspersed java 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>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 with interspersed 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");

Then it constructs an Address from the address parameters passed from RequestAdd.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 added to the addresses database:

    int rowsAffected = addressesDB.addAddress (address);

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

    if (rowsAffected == 1)
    {
%>
        <center>
            <h2><font color="#00cc00">Successfully Added Address</font></h2>
        </center>
<%
    }
    else
    {
%>
        <center>
            <h2><font color="#cc0000">Failed to Add Address</font></h2>
        </center>
<%
    }


Complete Source Code


<%@ page language="java" %>

<html>
    <head>
        <title>AddressBook: Adding Address</title>
    </head>
    <body bgcolor="#ffffee">
        <h1><center>AddressBook: Adding Address</center></h1>
        <table align="center" cellpadding="2" cellspacing="2" border="1" width="80%" bgcolor="#dddddd">
            <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
            (
                null,
                request.getParameter ("surname"),
                request.getParameter ("firstname"),
                request.getParameter ("street"),
                request.getParameter ("district"),
                request.getParameter ("city"),
                request.getParameter ("postcode")
            );
            int rowsAffected = addressesDB.addAddress (address);
            if (rowsAffected == 1)
            {
%>
                <center>
                    <h2><font color="#00cc00">Successfully Added Address</font></h2>
                </center>
<%
            }
            else
            {
%>
                <center>
                    <h2><font color="#cc0000">Failed to Add Address</font></h2>
                </center>
<%
            }
%>
        <hr>
        <center>
            <b><a href="Home.jsp">Back to AddressBook Home Page</a></b>
        </center>
    </body>
</html>