<% /* * add.jsp * * Adds a new entry to the address book. This is a JSP that serves 2 * functions. First of all, when called with no arguments, it will display a * table with a few input fields. The user should enter the name, address and * city of the new address entry to be added. When he/she submits this * information, it is sent to this page again. If it is successful, then the * user can continue adding new entries. If it is not, then the old data will * be displayed, and a warning message will be shown to the user. * * Version: $Revision: 1.1 $ $Date: 2005/02/08 10:10:55 $ * Authors: Ernst de Haan (ernst@jollem.com) */ %> <%@ page import="addressbook.ejb.*,java.io.*,java.util.*,javax.naming.*" %> <% // Make sure this page will not be cached by the browser response.addHeader("Pragma", "no-cache"); response.addHeader("Cache-Control", "no-store"); // We will send error messages to System.err, for verbosity. In a real // application you will probably not want this. PrintStream errorStream = System.err; // If we find any fatal error, we will store it in this variable. String error = null; // In a moment we will check if the name, address and city were passed to this // page. If they were not, then these variables will be set to contain the // empty string (""). String name = ""; String address = ""; String city = ""; // This variable indicates what function of this page is currently used. If // this page is called with parameters, then a new address entry should be // added. In that case this variable is true. boolean submitting = false; // We will first attempt to get the reference to the address book from the // session. The "list.jsp" page sets this attribute in the session. AddressBook addressBook = (AddressBook) session.getAttribute("AddressBook"); if (addressBook == null) { error = "No connection with the AddressBook bean established."; } else { // Attempt to get all 3 parameters from the session name = request.getParameter("name"); address = request.getParameter("address"); city = request.getParameter("city"); // If all 3 parameters are specified, then this is probably a submission by // this very page. Note that if the user left one of the fields blank, then // the corresponding parameter will be "", not null. if (name != null && address != null && city != null) { name = name.trim(); address = address.trim(); city = city.trim(); submitting = true; } } // In the following variable we will store a (non-fatal) warning message. This // message will be displayed in the page, but so will the submission form. String warning = null; if (submitting) { warning = ""; // If there is an empty name, address and/or city, then this will be noted // in the warning message. if ("".equals(name)) { warning = "No name specified. "; } if ("".equals(address)) { warning += "No address specified. "; } if ("".equals(city)) { warning += "No city specified."; } // If we don't have a warning message yet, then we will attempt to create // the new address entry. if ("".equals(warning)) { try { addressBook.create(name, address, city); // We will only get here if the previous statement succeeded. If it // did succeed, then the fields on the page should be empty, and the // warning message will be set to null, for easy comparison checking // later on. name = ""; address = ""; city = ""; // If we got this far, then there was no problem detected. warning = null; } catch (Exception e) { // Set the warning variable to indicate a problem. warning = "Unable to create new entry, caught: \"" + e.getClass().getName() + "\", message is: \"" + e.getMessage() + "\"."; } } } // Decide what the title will be. String title; if (error != null) { title = "Error"; } else { title = "Addressbook | Add entry"; } %> <%= title %>

<%= title %>

<% // If there was a fatal error, then display the error message if (error != null) { %>

<%= error %>
<% // Otherwise display a table with fields to be filled in. } else { // If there was a warning, then display it. if (warning != null) { %>
Warning: <%= warning %>
<% } /* if */ // Display the table with fields. There are two columns. The left column // contains the names of the fields, while the right column contains the // fields. %>

Name:
Address:
City:

<% } /* else */ %>

Back to list