| An Email Address Finder Example |
|
This example, named email, stores names and email addresses in a map file based on the java.util.TreeMap class defined in the JDK 1.2. The TreeMap class creates a data structure called a red-black tree. In the tree, data is stored with a key and a value. In this example, the name is the key and the email address is the value.
When you add an entry to the map file, you enter both a name (the key) and an email address (the value). You can look up or delete an email address by entering just a name. The name cannot be null because it is a key. If a user tries to enter a null name, the application throws an exception and displays an error page.
conditions are:
Every node has two children or is a leaf.
Every node is colored red or black.
Every leaf node is colored black.
If a node is red, then both of its children are black.
Every path from the root to a leaf contains the same number of black nodes.
The email example has three pages with HTML forms, two response files, one error page, and one JavaBeans component.
|
|
|
|
|
|
|
Adding a Name and Email Address (email.jsp)
________________________________________________________
<%@ page language="java" % >
<HTML
<%@ include file="copyright.html" %> <%@ page isThreadSafe="false" import="java.util.*, email.Map" errorPage="error.jsp" %> <jsp:useBean id="mymap" scope="session" class="email.Map" /> <jsp:setProperty name="mymap" property="name" param="name" /> <jsp:setProperty name="mymap" property="email" param="email" /> <% mymap.setAction( "add" ); %> <html> <head> <title> Email Finder</title> </head> <body bgcolor="#ffffff" background="background.gif" link="#000099"> <!-- the form table --> <form method="get"> <table border="0" cellspacing="0" cellpadding="5"> <tr> <td width="120"> </td> <td align="right"> <h1> Email Finder</h1> </td> </tr> <tr> <td width="120" align="right"> <b> Name</b> </td> <td align="left"> <input type="text" name="name" size="35"> </td> </tr> <tr> <td width="120" align="right"> <b>Email Address</b> </td> <td align="left"> <input type="text" name="email" size="35"> </td> </tr> <tr> <td width="120"> </td> <td align="right"> Please enter a name and an email address. </td> </tr> <tr> <td width="120"> </td> <td align="right"> <input type="submit" value="Add"> </td> </tr> <!-- here we call the put method to add the name and email address to the map file --> <% String rname = request.getParameter( "name" ); String remail = request.getParameter( "email" ); if ( rname != null) { mymap.put( rname, remail ); } %> <tr> <td width="120"> </td> <td align="right"> The map file has <font color="blue"> <%= mymap.size() %> </font> entries. </font> </td> </tr> <tr> <td width="120"> </td> <td align="right"> <a href="lookup.jsp"> Lookup</a> | <a href="delete.jsp"> Delete</a> </td> </tr> </table> </form> </body> </html>
__________________________________________________
Looking Up a Name in the Map File (lookup.jsp)
<%@ include file="copyright.html" %> <%@ page isThreadSafe="false" import="java.util.*, email.Map" errorPage="error.jsp" %> <jsp:useBean id="mymap" scope="session" class="email.Map" /> <jsp:setProperty name="mymap" property="name" param="name" /> <% mymap.setAction( "lookup" ); %> <html> <head> <title> Email Finder </title> </head> <body bgcolor="#ffffff" background="background.gif" link="#000099"> <form method="get"> <table border="0" cellspacing="0" cellpadding="5"> <tr> <td width="120"> </td> <td align="right"> <h1> Email Finder</h1> </td> </tr> <tr> <td width="120" align="right"> <b>Name</b> </td> <td align="left"> <input type="text" name="name" size="35"> </td> </tr> <tr> <td width="120"> </td> <td align="right"> Please enter a name for which <br>
you'd like an email address. </td>
</tr> <tr> <td width="120"> </td> <td align="right"> The map file has <font color="blue"> <%= mymap.size() %> </font> entries. </td>
</tr>
<tr> <td width="120"> </td> <td align="right"> <input type="submit" value="Lookup"> </td> </tr> <% if ( request.getParameter( "name" ) != null ) { %> <%@ include file="lookupresponse.jsp" %> <% } %> <tr> <td width="120"> </td> <td align="right"> <a href="email.jsp"> Add</a> | <a href="delete.jsp"> Delete</a> </td> </tr> </table> </body> </html>
_______________________________________________________
Displaying the Lookup Response (lookupresponse.jsp)
<%@ page import="java.util.*, email.Map" %> <tr> <td width="120"> </td> <td align="right"> <b> Success! </b> </td> </tr> <tr> <td width="120"> </td> <td align="right"> <jsp:getProperty name="mymap" property="name" /> <br> <jsp:getProperty name="mymap" property="email" /> </td> </tr>
______________________________________________________
Deleting an Email Address (delete.jsp)
<%@ include file="copyright.html" %> <%@ page isThreadSafe="false" import="java.util.*, email.Map" errorPage="error.jsp" %> <jsp:useBean id="mymap" scope="session" class="email.Map" /> <jsp:setProperty name="mymap" property="name" param="name" /> <!-- tags the JSP page so that we can display the right exception message later --> <% mymap.setAction( "delete" ); %> <html> <head> <title> Email Finder </title> </head> <body bgcolor="#ffffff" background="background.gif" link="#000099"> <form method="get"> <table border="0" cellspacing="0" cellpadding="5"> <tr> <td width="120"> </td> <td align="right"> <h1> Email Finder</h1> </td> </tr>
<tr>
<td width="120" align="right">
<b> Name</b> </td> <td align="left"> <input type="text" name="name" size="35"> </td> </tr> <tr> <td width="120"> </td> <td align="right"> Please enter a name you would like to delete. </td> </tr> <tr> <td width="120"> </td> <td align="right"> The map file has <font color="blue"> <%= mymap.size() %> </font> entries. </td> </tr> <tr> <td width="120"> </td> <td align="right"> <input type="submit" value="Delete"> </td> </tr> <!-- display the name and email address, then delete them from the map file --> <% if ( request.getParameter( "name" ) != null ) { %> <%@ include file="deleteresponse.jsp" %>< <% mymap.remove( request.getParameter("name") ) ; } %> <tr> <td width="120"> </td> <td align="right"> <a href="email.jsp">Add</a> | <a href="lookup.jsp"> Lookup</a> </td> </tr> </table> </body> </html>
_____________________________________________________
Displaying the Delete Response (deleteresponse.jsp)
<%@ page import="java.util.*, email.Map" %> <tr> <td width="120"> </td> <td align="right"> <b>Success!</b> </td> </tr> <tr> <td width="120"> </td> <td align="right"> <jsp:getProperty name="mymap" property="name" /> <br> <jsp:getProperty name="mymap" property="email" /> <br> <p> has been deleted from the map file. </td> </tr>
_______________________________________________________
Displaying Exception Messages (error.jsp)
<%@ include file="copyright.html" %> <%@ page isErrorPage="true" import="java.util.*, email.Map" %> <jsp:useBean id="mymap" scope="session" class="email.Map" /> <html> <head><title> Email Finder</title> </head> <body bgcolor="#ffffff" background="background.gif" link="#000099"> <table border="0" cellspacing="0" cellpadding="5"> <tr> <td width="150" align="right"> </td> <td align="right" valign="bottom"> <h1> Email Finder </h1> </td> </tr> <tr> <td width="150" align="right"> </td> <td align="right"> <b> Oops! an exception occurred.</b> </td> </tr> <tr> <td width="150" align="right"> </td> <td align="right"> The name of the exception is <%= exception.toString() %> . </td> </tr> <tr> <td width="150" align="right"> </td> <td align="right"> </td> </tr> <% if (mymap.getAction() == "delete" ) { %> <tr> <td width=150 align=right> </td> <td align=right> <b> This means that ...</b> <p>The entry you were trying to <font color="blue"> delete</font> is not in the map file <br> <b> <i> or</i> </b> <br> you did not enter a name to delete. <p> Want to try <a href="delete.jsp"> again</a> ? </td> </tr> <% } else if (mymap.getAction() == "lookup" ) { %> <tr> <td width="150" align="right"> </td> <td align="right"> <b> <i>This means that ...</b> </i> <p> the entry you were trying to <font color="blue">vlook up</font> is not in the map file, <b> <i> or</i> </b> <br> you did not enter a name to look up. <p> Want to try <a href="lookup.jsp"> again</a>? </td> </tr> <% } else if (mymap.getAction() == "add" ) { %> <tr> <td width="150" align="right"> </td> <td align="right"> <b><i> This means that ...</b> </i> <p> You were trying to <font color="blue"> add</font> an entry with a name of null. <br> The map file doesn't allow this. <p> Want to try <a href="email.jsp"> again</a>? </td> </tr> <% } %> </table>
__________________________________________________________
Creating the Map File (Map.java)
package email; import java.util.*; public class Map extends TreeMap { // In this treemap, name is the key and email is the value private String name, email, action; private int count = 0; public Map() { } public void setName( String formName ) { if ( formName != "" ) { name = formName; } } public String getName() return name; } public void setEmail( String formEmail ) { if ( formEmail != "" ) { email = formEmail; System.out.println( name ); // for debugging only System.out.println( email ); // for debugging only } } public String getEmail() { email = get(name).toString(); return email; } public void setAction( String pageAction ) { action = pageAction; } public String getAction() { return action; } }
| |
|
| |
| |
|
|
|