/* * Copyright (c) 2001, Xiaoping Jia. * All Rights Reserved. */ package expo2; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import org.apache.ecs.*; import org.apache.ecs.html.*; /** * Java Expo web app version 2. * The Java Expo web app registration servlet. * * @version 1.1 2001/04/20 * @since 1.0 * @author Xiaoping Jia */ public class JavaExpoRegister extends JavaExpoBase { public boolean handleRequest(HttpServletRequest request, HttpServletResponse response, List contents) throws ServletException, IOException { contents.add(new H1("Enterprise Java Expo Registration")); HttpSession session = request. getSession(); //System.out.println("Default session lifetiem=" + session.getMaxInactiveInterval()); String submit = request.getParameter("submit"); RegistrationForm form = new RegistrationForm(); if (submit == null) { // you have come to the page from the menu Form htmlform = new Form(response.encodeURL(urlPrefix + "JavaExpoRegister"), "post"); contents.add(form.makeForm(htmlform)); } else { //you have come here after hitting the submit button form.fillForm(request); String[] missing = form.validateForm(); if (missing == null) { session.setAttribute("registration form", form); // add the form to the session List confirmation = form.processForm(); contents.addAll(confirmation); contents.add(new Form(response.encodeURL(urlPrefix + "JavaExpoPresentations"), "post") .addElement(new Input(Input.submit, "submit", "Select presentations"))); } else { contents.add("The follwing required field" + (missing.length > 1 ? "s are" : " is") + " missing"); UL list = new UL(); for (int i = 0; i < missing.length; i++) { list.addElement(new LI(missing[i])); } contents.add(list); Form htmlform = new Form(response.encodeURL(urlPrefix + "JavaExpoRegister"), "post"); contents.add(form.makeForm(htmlform)); } } return true; } /** * A class represents the registration form for the Java Expo */ class RegistrationForm implements Serializable { // fields in the registration form String salutation; String firstName, lastName; String email; String company; String business; String street, city, state, country, postalCode; String[] interests; String comments; /** * Build an HTML form that consists of the input fields of the registration form. * If the a field in the RegistrationForm object contains a non-null value, * the value will be filled in the corresponding input field in the HTML form. * * @return an HTML form with fields corresponding to the fields of the RegistrationForm class. */ Form makeForm(Form form) { Input rbMr, rbMrs, rbMs, rbDr; Input inFirstName, inLastName, inEmail, inCompany, inStreet, inCity, inState, inCountry, inPostalCode; Input cbServlets, cbJSP, cbEJB, cbJDBC, cbJavaMail, cbJNDI, cbJMS, cbJTA, cbJTS, cbIDL, cbRMI, cbConnector; TextArea taComments; form .addElement(new Table() .addElement(new TR() .addElement(new TD().setColSpan(2) .addElement(rbMr = new Input(Input.radio, "salutation", "Mr.") .addElement(" Mr. ")) .addElement(rbMrs = new Input(Input.radio, "salutation", "Mrs.") .addElement(" Mrs. ")) .addElement(rbMs = new Input(Input.radio, "salutation", "Ms.") .addElement(" Ms. ")) .addElement(rbDr = new Input(Input.radio, "salutation", "Dr.") .addElement(" Dr. ")))) .addElement(new TR() .addElement(new TD("First name *")) .addElement(new TD() .addElement(inFirstName = new Input(Input.text, "firstname", "") .setSize(20))) .addElement(new TD("Last name *")) .addElement(new TD() .addElement(inLastName = new Input(Input.text, "lastname", "") .setSize(20)))) .addElement(new TR() .addElement(new TD("E-mail *")) .addElement(new TD() .addElement(inEmail = new Input(Input.text, "email", "") .setSize(20))) .addElement(new TD("Company")) .addElement(new TD() .addElement(inCompany = new Input(Input.text, "company", "") .setSize(20)))) .addElement(new TR() .addElement(new TD("Primary area of business").setColSpan(2))) .addElement(new TR() .addElement(new TD().setColSpan(4) .addElement(new Table() .addElement(new TR() .addElement(new TD(" ")) .addElement(new TD() .addElement(new Select("business") .addElement(new Option("Software Development") .addElement(" Software Development ")) .addElement(new Option("Consulting") .addElement(" Consulting ")) .addElement(new Option("E-Commerce") .addElement(" E-Commerce ")) .addElement(new Option("Financial") .addElement(" Financial ")) .addElement(new Option("Education") .addElement(" Education ")) .addElement(new Option("Government") .addElement(" Government ")) .addElement(new Option("Research") .addElement(" Research ")) .addElement(new Option("Unknown") .addElement(" Other ")))))))) .addElement(new TR() .addElement(new TD("Mailing address:").setColSpan(4))) .addElement(new TR() .addElement(new TD().setColSpan(4) .addElement(new Table() .addElement(new TR() .addElement(new TD(" ")) .addElement(new TD("Street")) .addElement(new TD() .addElement(inStreet = new Input(Input.text, "street", "") .setSize(20))) .addElement(new TD("City")) .addElement(new TD() .addElement(inCity = new Input(Input.text, "city", "") .setSize(20)))) .addElement(new TR() .addElement(new TD()) .addElement(new TD("State")) .addElement(new TD() .addElement(inState = new Input(Input.text, "state", "") .setSize(20))) .addElement(new TD("Country")) .addElement(new TD() .addElement(inCountry = new Input(Input.text, "country", "") .setSize(20)))) .addElement(new TR() .addElement(new TD()) .addElement(new TD("Postal Code")) .addElement(new TD() .addElement(inPostalCode = new Input(Input.text, "postal", "") .setSize(20))))))) .addElement(new TR() .addElement(new TD("Interest areas:").setColSpan(4))) .addElement(new TR() .addElement(new TD().setColSpan(4) .addElement(new Table() .addElement(new TR() .addElement(new TD(" ")) .addElement(new TD() .addElement(cbServlets = new Input(Input.checkbox, "interests", "Servlets") .addElement(" Servlets "))) .addElement(new TD() .addElement(cbJSP = new Input(Input.checkbox, "interests", "JSP") .addElement(" JSP "))) .addElement(new TD() .addElement(cbEJB = new Input(Input.checkbox, "interests", "EJB") .addElement(" EJB "))) .addElement(new TD() .addElement(cbJDBC = new Input(Input.checkbox, "interests", "JDBC") .addElement(" JDBC ")))) .addElement(new TR() .addElement(new TD(" ")) .addElement(new TD() .addElement(cbJavaMail = new Input(Input.checkbox, "interests", "JavaMail") .addElement(" JavaMail "))) .addElement(new TD() .addElement(cbJNDI = new Input(Input.checkbox, "interests", "JNDI") .addElement(" JNDI "))) .addElement(new TD() .addElement(cbJMS = new Input(Input.checkbox, "interests", "JMS") .addElement(" JMS "))) .addElement(new TD() .addElement(cbJTA = new Input(Input.checkbox, "interests", "JTA") .addElement(" JTA ")))) .addElement(new TR() .addElement(new TD(" ")) .addElement(new TD() .addElement(cbJTS = new Input(Input.checkbox, "interests", "JTS") .addElement(" JTS "))) .addElement(new TD() .addElement(cbIDL = new Input(Input.checkbox, "interests", "IDL") .addElement(" IDL "))) .addElement(new TD() .addElement(cbRMI = new Input(Input.checkbox, "interests", "RMI-IIOP") .addElement(" RMI-IIOP "))) .addElement(new TD() .addElement(cbConnector = new Input(Input.checkbox, "interests", "J2EE Connector") .addElement(" J2EE Connector "))))))) .addElement(new TR() .addElement(new TD("Comments:").setColSpan(4))) .addElement(new TR() .addElement(new TD().setColSpan(4) .addElement(taComments = new TextArea("comments", 3, 80)))) .addElement(new TR() .addElement(new TD().setColSpan(2) .addElement(new Input(Input.submit, "submit", "Register")))) .addElement(new TR() .addElement(new TD().setColSpan(2) .addElement("* Indicates required fields.")))); if ("Mrs.".equals(salutation)) { rbMrs.setChecked(true); } else if ("Ms.".equals(salutation)) { rbMs.setChecked(true); } else if ("Dr.".equals(salutation)) { rbDr.setChecked(true); } else { rbMr.setChecked(true); } if (firstName != null) { inFirstName.setValue(firstName); } if (lastName != null) { inLastName.setValue(lastName); } if (email != null) { inEmail.setValue(email); } if (company != null) { inCompany.setValue(company); } if (street != null) { inStreet.setValue(street); } if (city != null) { inCity.setValue(city); } if (state != null) { inState.setValue(state); } if (country != null) { inCountry.setValue(country); } if (postalCode != null) { inPostalCode.setValue(postalCode); } if (interests != null) { for (int i = 0; i < interests.length; i++) { if (interests[i].equals("Servlets")) { cbServlets.setChecked(true); } else if (interests[i].equals("JSP")) { cbJSP.setChecked(true); } else if (interests[i].equals("EJB")) { cbEJB.setChecked(true); } else if (interests[i].equals("JDBC")) { cbJDBC.setChecked(true); } else if (interests[i].equals("JavaMail")) { cbJavaMail.setChecked(true); } else if (interests[i].equals("JNDI")) { cbJNDI.setChecked(true); } else if (interests[i].equals("JMS")) { cbJMS.setChecked(true); } else if (interests[i].equals("JTA")) { cbJTA.setChecked(true); } else if (interests[i].equals("JTS")) { cbJTS.setChecked(true); } else if (interests[i].equals("IDL")) { cbIDL.setChecked(true); } else if (interests[i].equals("RMI-IIOP")) { cbRMI.setChecked(true); } else if (interests[i].equals("J2EE Connector")) { cbConnector.setChecked(true); } } } return form; } /** * Retrieves the values posted from the HTML form and assigns the values * to the corresponding fields of the RegistrationForm object. * * @param request the HTTP request */ void fillForm(HttpServletRequest request) { salutation = request.getParameter("salutation"); firstName = request.getParameter("firstname"); if (firstName != null) { firstName = firstName.trim(); } lastName = request.getParameter("lastname"); if (lastName != null) { lastName = lastName.trim(); } email = request.getParameter("email"); if (email != null) { email = email.trim(); } company = request.getParameter("company"); if (company != null) { company = company.trim(); } business = request.getParameter("business"); if (business != null) { business = business.trim(); } street = request.getParameter("street"); if (street != null) { street = street.trim(); } city = request.getParameter("city"); if (city != null) { city = city.trim(); } state = request.getParameter("state"); if (state != null) { state = state.trim(); } country = request.getParameter("country"); if (country != null) { country = country.trim(); } postalCode = request.getParameter("postal"); if (postalCode != null) { postalCode = postalCode.trim(); } comments = request.getParameter("comments"); if (comments != null) { comments = comments.trim(); } interests = request.getParameterValues("interests"); } /** * Checks whether all the required fields contains non-null values. * Returns null if all the required fields contains non-null values. * Otherwise, returns an array of the names of the required fields that contains * null values. * * @return an array of missing field names. */ String[] validateForm() { List missing = new ArrayList(); if (firstName == null || firstName.length() == 0) { missing.add("first name"); } if (lastName == null || lastName.length() == 0) { missing.add("last name"); } if (email == null || email.length() == 0) { missing.add("email"); } if (missing.size() > 0) { String[] arr = new String[missing.size()]; for (int i = 0; i < missing.size(); i++) { arr[i] = (String) missing.get(i); } return arr; } return null; } /** * Builds a HTML page that confirms the registration. * * @return a list of HTML elements. */ List processForm() { List contents = new ArrayList(); contents.add(new H1("Thank you! Your registration is confirmed.")); contents.add(salutation + " " + firstName + " " + lastName); contents.add(new BR()); contents.add(company); contents.add(new BR()); contents.add(street); contents.add(new BR()); contents.add(city + " " + state + " " + postalCode); contents.add(new BR()); contents.add(country); contents.add(new BR()); contents.add("Email: " + email); contents.add(new BR()); contents.add("Primary area of business: " + business); contents.add(new BR()); contents.add("Interest areas"); if (interests != null) { UL list = new UL(); for (int i = 0; i < interests.length; i++) { list.addElement(new LI(interests[i])); } contents.add(list); } contents.add("Commetns: " + comments); return contents; } } }