AddressBook/web/Home.jsp

Notes

Purpose
The purpose of Home.jsp is to produce a web page with a table displaying the contents of the addresses database, with links for adding new addresses to the database [RequestAdd.jsp]; deleting specific addresses from the database [RequestDelete.jsp]; and modifying specific addresses on the addresses database [RequestModify.jsp]. Figure 5 shows an example of AddressBook's home web page.

[Figure05]


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

    <html>
        <head>
            <title>AddressBook: Home Page</title>
        </head>
        <body bgcolor="#ffffee">
            <h1><center>AddressBook: Home Page</center></h1>

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

            <br>
            <hr>
            <center>
                <b><a href="RequestAdd.jsp">Add a New Address</a></b>
        </center>
        </body>
    </html>


Display the Table
This html code displays the table and its headings:

    <table align="center" cellpadding="2" cellspacing="2" border="1" width="80%" bgcolor="#dddddd">
        <tr>
            <th>Name</th>
            <th>Address</th>
            <th>Action</th>
            <th>ID</th>
        </tr>

        ... code to loop through all the table's rows goes here ...

    </table>


Loop Through All the Table's Rows
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 the contents of the database are retrieved as a Collection:

    Collection addresses = addressesDB.getAddresses ();

If the Collection is not null, and it contains some addresses [ie: its size is greater than 0], then the Collection is processed, retrieving each address in turn:

    if (addresses != null)
    {
        if (addresses.size () > 0)
        {
            for (Iterator iterator = addresses.iterator(); iterator.hasNext(); )
            {
                AddressBook.Address address = (AddressBook.Address) iterator.next ();

                ... code to display an address in a table row goes here ...

            }
        }
    }


Display an Address in a Table Row
This html code with interspersed java code displays an address as a row in a table. The java code simply retrieves individual fields from the address: address.getFullname () returns the fullname; address.getFulladdress () returns the fulladdress; and address.getId () returns the id. In the table's first column goes the full name; in the second column goes the full address; in the third column goes a link to RequestDelete.jsp, the word "or" and a link to RequestModify.jsp; and in the final column goes the id:

    <tr>
        <td>
            <%= address.getFullname () %>
        </td>
        <td>
            <%= address.getFulladdress () %>
        </td>
        <td align="center">
            <b><a href="RequestDelete.jsp?id=<%= address.getId () %>">Delete</a></b>
            or
            <b><a href="RequestModify.jsp?id=<%= address.getId () %>">Modify</a></b>
        </td>
        <td align="center">
            <%= address.getId () %>
        </td>
    </tr>

When the "Delete" link is selected the id parameter is passed to RequestDelete.jsp.

When the "Modify" link is selected the id parameter is passed to RequestModify.jsp.


Complete Source Code


<%@ page language="java" import="java.util.*" %>

<html>
    <head>
        <title>AddressBook: Home Page</title>
    </head>
    <body bgcolor="#ffffee">
        <h1><center>AddressBook: Home Page</center></h1>
        <table align="center" cellpadding="2" cellspacing="2" border="1" width="80%" bgcolor="#dddddd">
            <tr>
                <th>Name</th>
                <th>Address</th>
                <th>Action</th>
                <th>ID</th>
            </tr>
<%
            AddressBook.AddressesDB addressesDB = (AddressBook.AddressesDB) application.getAttribute ("addressesDB");
            Collection addresses = addressesDB.getAddresses ();
            if (addresses != null)
            {
                if (addresses.size () > 0)
                {
                    for (Iterator iterator = addresses.iterator(); iterator.hasNext(); )
                    {
                        AddressBook.Address address = (AddressBook.Address) iterator.next ();
%>
                        <tr>
                            <td>
                                <%= address.getFullname () %>
                            </td>
                            <td>
                                <%= address.getFulladdress () %>
                            </td>
                            <td align="center">
                                <b><a href="RequestDelete.jsp?id=<%= address.getId () %>">Delete</a></b>
                                or
                                <b><a href="RequestModify.jsp?id=<%= address.getId () %>">Modify</a></b>
                            </td>
                            <td align="center">
                                <%= address.getId () %>
                            </td>
                        </tr>
<%
                    }
                }
            }
%>
        </table>
        <br>
        <hr>
        <center>
            <b><a href="RequestAdd.jsp">Add a New Address</a></b>
       </center>
    </body>
</html>