<% /* * edit.jsp * * Allows the user to edit an existing address entry. This is a JSP that * serves 2 functions, similar to "add.jsp". First of all, when called with * only a "name" argument, it will display a table with a few input fields. * One for the address, and one for the city. These fields are filled in, and * they can be changed by the user. * * When he/she submits this information, it is sent to this page again. If the * data can successfully be changed, then the user will see the new data in * this page, with no error message. * * Version: $Revision: 1.2 $ $Date: 2000/09/06 15:22:43 $ * 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. We will send a few log // messages to System.out. PrintStream errorStream = System.err; // If we find any fatal error, we will store it in the "error" variable. If we // need to store the exception too, we will store it in "exception". String error = null; Exception exception = null; // 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; // 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 = ""; // 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(); warning = ""; if ("".equals(name)) { warning = "Name not specified. "; } if ("".equals(address)) { warning += "Address not specified. "; } if ("".equals(city)) { warning += "City not specified."; } if ("".equals(warning)) { warning = null; try { AddressEntry entry = addressBook.findByPrimaryKey(name); entry.setAddress(address); entry.setCity(city); } catch (Exception e) { exception = e; error = "Caught \"" + exception.getClass().getName() + "\" while attempting to get the address entry with " + "name \"" + name + "\"."; errorStream.println(error); exception.printStackTrace(errorStream); } } // If the name is null, then something is definitely wrong. When this page // is called from "list.jsp", the name is always specified. } else if (name == null) { error = "No name specified as a parameter."; // If only name is non-null, and address and city are null, then we will // attempt to obtain the address and city from the entity bean. } else { try { AddressEntry entry = addressBook.findByPrimaryKey(name); address = entry.getAddress(); city = entry.getCity(); } catch (Exception e) { exception = e; error = "Caught \"" + exception.getClass().getName() + "\" while " + "attempting to get the address entry with name \"" + name + "\"."; errorStream.println(error); exception.printStackTrace(errorStream); } } } // Decide what the title will be. String title; if (error != null) { title = "Error"; } else { title = "Addressbook | Edit 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: <%= name %>
Address:
City:

<% } /* else */ %>

Back to list