package downey.complexBean; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import downey.PresetCheckedAndSelected; import downey.BeanUtilities; import downey.complexBean.beans.BidInfo; /** Example of simplified form processing. Shows two features: *
    *
  1. Automatically filling in a bean based on the * incoming request parameters. *
  2. Using the same servlet both to generate the input * form and to process the results. That way, when * fields are omitted, the servlet can redisplay the * form without making the user reenter previously * entered values. * *

    * Taken from Core Servlets and JavaServer Pages 2nd Edition * from Prentice Hall and Sun Microsystems Press, * http://www.coreservlets.com/. * © 2003 Marty Hall; may be freely used or adapted. */ public class ComplexBidServlet extends HttpServlet { /** Try to populate a bean that represents information * in the form data sent by the user. If this data is * complete, show the results. If the form data is * missing or incomplete, display the HTML form * that gathers the data. */ private static String[] optionsValues = {"Gift Wrapped", "Express Delivery", "Request Catalog"}; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BidInfo bid = new BidInfo(); BeanUtilities.populateBean(bid, request); if (bid.isComplete()) { // All required form data was supplied: show result. showBid(request, response, bid); } else { // Form data was missing or incomplete: redisplay form. showEntryForm(request, response, bid, true); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BidInfo bid = new BidInfo(); BeanUtilities.populateBean(bid, request); showEntryForm(request, response, bid, false); } /** All required data is present: show the results page. */ private void showBid(HttpServletRequest request, HttpServletResponse response, BidInfo bid) throws ServletException, IOException { submitBid(bid); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Bid Submitted"; out.println (DOCTYPE + "\n" + "\n" + "" + title + "\n" + "

    \n" + "

    " + title + "

    \n" + "Your bid is now active. If your bid is successful,\n" + "you will be notified within 24 hours of the close\n" + "of bidding.\n" + "

    \n" + "\n" + "
    " + bid.getItemName() + "\n" + "
    Item ID: " + bid.getItemID() + "\n" + "
    Name: " + bid.getBidderName() + "\n" + "
    Email address: " + bid.getEmailAddress() + "\n" + "
    Bid price: $" + bid.getBidPrice() + "\n" + "
    Options: "); String[] options = bid.getOptions(); if (options != null) { out.println("
      \n"); for (int i=0; i" + options[i] + "\n"); } out.println("
    \n"); } out.println("

    "); } /** If the required data is totally missing, show a blank * form. If the required data is partially missing, * warn the user, fill in form fields that already have * values, and prompt user for missing fields. */ private void showEntryForm(HttpServletRequest request, HttpServletResponse response, BidInfo bid, boolean isFromPost) throws ServletException, IOException { //boolean isPartlyComplete = bid.isPartlyComplete(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Welcome to Auctions-R-Us. Please Enter Bid."; out.println (DOCTYPE + "\n" + "\n" + "\n" + "" + title + "\n" + "
    \n" + "

    " + title + "

    \n" + warning(isFromPost) + "
    \n" + inputElement("Item ID", "itemID", bid.getItemID(), isFromPost) + inputElement("Item Name", "itemName", bid.getItemName(), isFromPost) + inputElement("Your Name", "bidderName", bid.getBidderName(), isFromPost) + inputElement("Your Email Address", "emailAddress", bid.getEmailAddress(), isFromPost) + inputElement("Amount Bid", "bidPrice", bid.getBidPrice(), isFromPost) + inputGroup("checkbox", "options", optionsValues, bid) + "
    \n" + "
    "); } private void submitBid(BidInfo bid) { // Some application-specific code to record the bid. // The point is that you pass in a real object with // properties populated, not a bunch of strings. } private String warning(boolean isFromPost) { if(isFromPost) { return("

    Required Data Missing! " + "Enter and Resubmit.

    \n"); } else { return(""); } } /** Create a textfield for input, prefaced by a prompt. * If this particular textfield is missing a value but * other fields have values (i.e., a partially filled form * was submitted), then add a warning telling the user that * this textfield is required. */ private String inputElement(String prompt, String name, String value, boolean isFromPost) { String message = ""; String style = ""; if (isFromPost && ((value == null) || value.equals(""))) { message = "Required field! "; style = " class='error'"; } return(message + prompt + ": " + "
    \n"); } private String inputElement(String prompt, String name, double value, boolean isFromPost) { String num; if (value == 0.0) { num = ""; } else { num = String.valueOf(value); } return(inputElement(prompt, name, num, isFromPost)); } private String inputGroup(String type, String name, String[] values, BidInfo bid) { String result = ""; if (values == null) return result; for (int pos=0; pos" + values[pos] + " \n"; } return result; } private final String DOCTYPE = "\n"; }