AddressBook/web/DoDelete.jsp

Notes

Purpose
The purpose of DoDelete.jsp is to produce a web page with a table displaying the address whose id parameter was passed from RequestDelete.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 14 shows an example of the DoDelete web page.

[Figure14]


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: Deleting Address</title>
        </head>
        <body bgcolor="#ffffee">
            <h1><center>AddressBook: Deleting Address</center></h1>

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

            ... 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>


Retrieve Address Data to be Deleted
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 reads the address whose id has been passed as a parameter to this page by RequestDelete.jsp from the addresses database:

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


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 address:

    <table align="center" cellpadding="2" cellspacing="2" border="1" width="80%" bgcolor="#dddddd">
        <tr>
            <th>ID:</th>
            <td><%= address.getId () %></td>
        </tr>
        <tr>
            <th>Surname:</th>
            <td><%= address.getSurname () %></td>
        </tr>
        <tr>
            <th>Firstname:</th>
            <td><%= address.getFirstname () %></td>
        </tr>
        <tr>
            <th>Street:</th>
            <td><%= address.getStreet () %></td>
        </tr>
        <tr>
            <th>District:</th>
            <td><%= address.getDistrict () %></td>
        </tr>
        <tr>
            <th>City:</th>
            <td><%= address.getCity () %></td>
        </tr>
        <tr>
            <th>Postcode:</th>
            <td><%= address.getPostcode () %></td>
        </tr>
    </table>


Update the Database and Display Success or Failure
This java code with interspersed html code deletes the address whose id whose parameter was passed from RequestDelete.jsp:

    int rowsAffected = addressesDB.deleteAddress (request.getParameter ("id"));

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

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


Complete Source Code


<%@ page language="java" %>

<html>
    <head>
        <title>AddressBook: Deleting Address</title>
    </head>
    <body bgcolor="#ffffee">
        <h1><center>AddressBook: Deleting Address</center></h1>
<%
        AddressBook.AddressesDB addressesDB = (AddressBook.AddressesDB) application.getAttribute ("addressesDB");
        AddressBook.Address address = (AddressBook.Address) addressesDB.getAddress (request.getParameter ("id"));
        if (address != null)
        {
%>
            <table align="center" cellpadding="2" cellspacing="2" border="1" width="80%" bgcolor="#dddddd">
                <tr>
                    <th>ID:</th>
                    <td><%= address.getId () %></td>
                </tr>
                <tr>
                    <th>Surname:</th>
                    <td><%= address.getSurname () %></td>
                </tr>
                <tr>
                    <th>Firstname:</th>
                    <td><%= address.getFirstname () %></td>
                </tr>
                <tr>
                    <th>Street:</th>
                    <td><%= address.getStreet () %></td>
                </tr>
                <tr>
                    <th>District:</th>
                    <td><%= address.getDistrict () %></td>
                </tr>
                <tr>
                    <th>City:</th>
                    <td><%= address.getCity () %></td>
                </tr>
                <tr>
                    <th>Postcode:</th>
                    <td><%= address.getPostcode () %></td>
                </tr>
            </table>
<%
            int rowsAffected = addressesDB.deleteAddress (request.getParameter ("id"));
            if (rowsAffected == 1)
            {
%>
                <center>
                    <h2><font color="#00cc00">Successfully Deleted Address <%= request.getParameter ("id") %></font></h2>
                </center>
<%
            }
            else
            {
%>
                <center>
                    <h2><font color="#cc0000">Failed to Delete Address <%= request.getParameter ("id") %></font></h2>
                </center>
<%
            }
        }
        else
        {
%>
            <center>
                <h2><font color="#cc0000">Could Not Find Address <%= request.getParameter ("id") %></font></h2>
            </center>
<%
        }
%>
        <hr>
        <center>
            <b><a href="Home.jsp">Back to AddressBook Home Page</a></b>
        </center>
    </body>
</html>