Web Applications as Java Servlets by Brad Cox Listing One package com.sdi.field; import gnu.regexp.*; public class Zipcode extends Field implements Validatable { private final static RE re = re("\\d{5}([ -_]?\\d{4})?"); private final static String msg = "A 5 or 9 digit zipcode is required"; public final static Zipcode Null = new Zipcode(null); public Zipcode(String value) { super(value); } public boolean setValue(String v) { setValue(v, true, ""); return requireNonNull() & requireMatch(re, msg); } } Listing Two public class AddressBean extends BeanImpl { private Identifier accountID = Identifier.Null; private Street street = Street.Null; private City city = City.Null; private USState state = USState.Null; private Zipcode zipcode = Zipcode.Null; private Phone phone = Phone.Null; public final static AddressBean Null = new AddressBean( Identifier.Null, Street.Null, City.Null, USState.Null, Zipcode.Null, Phone.Null ); Listing Three public final void controller() throws Exception { try { AccountBean thisAccount = (AccountBean)site.getAccount(this); redirect(DemoSite.Account); return; // should never happen } catch (SessionExpiredException e) { /* empty */ } Op op = (Op)getField("op", Op.Null); Identifier identifier = (Identifier)getField("identifier", Identifier.Null); if ( op.isValid() && identifier.isValid()) { try { Connection connection = ConnectionPool.getConnection(); AccountBean account = AccountBean.load(connection, identifier); site.setAccount(account, this); redirect(DemoSite.Account); } catch (Exception e) { identifier.setValid(false, "This identifier was not found"); sendPage(viewLogin(identifier)); } } else sendPage(viewLogin(identifier)); } Listing Four public void controller() throws Exception { AccountBean thisAccount = (AccountBean)site.getAccount(this); AddressBean address = thisAccount.getAddress(); Op op = (Op)getField("op", Op.Null); Street street = (Street)getField("street", address.getStreet()); City city = (City)getField("city", address.getCity()); USState state = (USState)getField("state", address.getState()); Zipcode zipcode = (Zipcode)getField("zipcode", address.getZipcode()); Phone phone = (Phone)getField("phone", address.getPhone()); if ( op.isValid() && street.isValid() && city.isValid() && state.isValid() && zipcode.isValid() && phone.isValid()) { address.setAddress( street, city, state, zipcode, phone); address.save(thisAccount.getConnection()); forward(DemoSite.Account); } else sendPage(viewAddress( op, street, city, state, zipcode, phone)); } Listing Five public class DemoSite extends SiteImpl implements Site { public static final Page Home = newStaticPage( "Home", null, Role.Null, "/html/index.htm", "Demo home page" ); public final static DynamicPage Account = newDynamicPage( "Account", AccountView.class, "Account", Registered, "To your account" ); public final static DynamicPage AccountRegistration = newDynamicPage( "AccountRegistration", AccountRegistrationView.class, "Registration", Role.Null, "Account Registration" ); public final static DynamicPage EditAddress= newDynamicPage( "EditAddress", EditAddressView.class, "Address", Registered, "Modify your address and/or phone number" ); public final static DynamicPage Login = newDynamicPage( "Login", LoginView.class, "Login", Role.Null, "Login procedure" ); public final static DynamicPage Logout = newDynamicPage( "Logout", LogoutView.class, "Logout", Role.Null, "Logout" ); public final static DynamicPage Refuse = newDynamicPage( "Refuse", RefuseView.class, "Permission Denied", Role.Null, "Permission denied" ); } Listing Six public static AddressBean load(Connection connection, Identifier accountID) throws Exception { String sql = "select * from DemoAddress " + "where accountID=\"" + accountID + "\""; try { Statement stmt = connection.createStatement(); ResultSet s = stmt.executeQuery(sql); if (!s.next()) throw new Exception("couldn't do " + sql); return new AddressBean( accountID, new Street(s.getString("street")), new City(s.getString("city")), new USState(s.getString("state")), new Zipcode(s.getString("zipcode")), new Phone(s.getString("phone")) ); } catch (SQLException e) { throw new Exception("SQLException in " + sql + "\n\t" + e); } } Listing Seven public Identifier save(Connection connection) throws Exception { String sql = "replace into DemoAddress set " + "accountID=?, " + "street=?, " + "city=?, " + "state=?, " + "zipcode=?, " + "phone=?"; try { PreparedStatement s = connection.prepareStatement(sql); s.setString(1, accountID.toString()); s.setString(2, street.toString()); s.setString(3, city.toString()); s.setString(4, state.toString()); s.setString(5, zipcode.toString()); s.setString(6, phone.toString()); int n = s.executeUpdate(); s.close(); return accountID; } catch (Throwable e) { throw new Exception("Throwable in " + sql + "\n\t" + e); } } 4