Servlet/Jsp实现当参数确实或缺失重新显示输入表单

(1)在有些时候我们的表单需要用户填写,但是没有填写就提交了,我们需要保留已经填写的数据然后再让用户填写没有填写的数据

(2)以一个处理拍卖竞标的servlet来实现功能:

BidInfo.java

package com.lc.ch04jingbiao;

import com.lc.ch04Biaodanshuju.ServletUtilities;


public class BidInfo {
  private String itemID = "";
  private String itemName = "";
  private String bidderName = "";
  private String emailAddress = "";
  private double bidPrice = 0;
  private boolean autoIncrement = false;

  public String getItemName() {
    return(itemName);
  }

  public void setItemName(String itemName) {
    this.itemName = ServletUtilities.filter(itemName);
  }

  public String getItemID() {
    return(itemID);
  }

  public void setItemID(String itemID) {
    this.itemID = ServletUtilities.filter(itemID);
  }
  
  public String getBidderName() {
    return(bidderName);
  }

  public void setBidderName(String bidderName) {
    this.bidderName =  ServletUtilities.filter(bidderName);
  }

  public String getEmailAddress() {
    return(emailAddress);
  }

  public void setEmailAddress(String emailAddress) {
    this.emailAddress = ServletUtilities.filter(emailAddress);
  }

  public double getBidPrice() {
    return(bidPrice);
  }

  public void setBidPrice(double bidPrice) {
    this.bidPrice = bidPrice;
  }

  public boolean isAutoIncrement() {
    return(autoIncrement);
  }

  public void setAutoIncrement(boolean autoIncrement) {
    this.autoIncrement = autoIncrement;
  }

  /**判断是否完成
  */

  public boolean isComplete() {
    return(hasValue(getItemID()) &&
           hasValue(getItemName()) &&
           hasValue(getBidderName()) &&
           hasValue(getEmailAddress()) &&
           (getBidPrice() > 0));
  }
//判断是否有 没有填写的项

  public boolean isPartlyComplete() {
    boolean flag =
           (hasValue(getItemID()) ||
           hasValue(getItemName()) ||
           hasValue(getBidderName()) ||
           hasValue(getEmailAddress()) ||
           (getBidPrice() > 0) ||
           isAutoIncrement());
    return(flag);
  }

  private boolean hasValue(String val) {
    return((val != null) && (!val.equals("")));
  }
}


BidServlet代码如下:

package com.lc.ch04jingbiao;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lc.ch04Biaodanshuju.BeanUtilities;


public class BidServlet extends HttpServlet {

  public void doGet(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);
    }
  }

  /** 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 +
       "<HTML>\n" +
       "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
       "<BODY BGCOLOR=\"#FDF5E6\"><CENTER>\n" +
       "<H1>" + title + "</H1>\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" +
       "<P>\n" +
       "<TABLE BORDER=1>\n" +
       "  <TR><TH BGCOLOR=\"BLACK\"><FONT COLOR=\"WHITE\">" +
       bid.getItemName() + "</FONT>\n" +
       "  <TR><TH>Item ID: " +
       bid.getItemID() + "\n" +
       "  <TR><TH>Name: " +
       bid.getBidderName() + "\n" +
       "  <TR><TH>Email address: " +
       bid.getEmailAddress() + "\n" +
       "  <TR><TH>Bid price: $" +
       bid.getBidPrice() + "\n" +
       "  <TR><TH>Auto-increment price: " +
       bid.isAutoIncrement() + "\n" +
       "</TABLE></CENTER></BODY></HTML>");
  }

  private void showEntryForm(HttpServletRequest request,
                      HttpServletResponse response,
                      BidInfo bid)
      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 +
       "<HTML>\n" +
       "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
       "<BODY BGCOLOR=\"#FDF5E6\"><CENTER>\n" +
       "<H1>" + title + "</H1>\n" +
       warning(isPartlyComplete) +
       "<FORM>\n" +
       inputElement("Item ID", "itemID",
                    bid.getItemID(), isPartlyComplete) +
       inputElement("Item Name", "itemName",
                    bid.getItemName(), isPartlyComplete) +
       inputElement("Your Name", "bidderName",
                    bid.getBidderName(), isPartlyComplete) +
       inputElement("Your Email Address", "emailAddress",
                    bid.getEmailAddress(), isPartlyComplete) +
       inputElement("Amount Bid", "bidPrice",
                    bid.getBidPrice(), isPartlyComplete) +
       checkbox("Auto-increment bid to match other bidders?",
                "autoIncrement", bid.isAutoIncrement()) +
       "<INPUT TYPE=\"SUBMIT\" VALUE=\"Submit Bid\">\n" +
       "</CENTER></BODY></HTML>");
  }

  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 isFormPartlyComplete) {
    if(isFormPartlyComplete) {
      return("<H2>Required Data Missing! " +
             "Enter and Resubmit.</H2>\n");
    } else {
      return("");
    }
  }

  
  private String inputElement(String prompt,
                              String name,
                              String value,
                              boolean shouldPrompt) {
    String message = "";
    if (shouldPrompt && ((value == null) || value.equals(""))) {
      message = "<B>Required field!</B> ";
    }
    return(message + prompt + ": " +
           "<INPUT TYPE=\"TEXT\" NAME=\"" + name + "\"" +
           " VALUE=\"" + value + "\"><BR>\n");
  }

  private String inputElement(String prompt,
                              String name,
                              double value,
                              boolean shouldPrompt) {
    String num;
    if (value == 0.0) {
      num = "";
    } else {
      num = String.valueOf(value);
    }
    return(inputElement(prompt, name, num, shouldPrompt));
  }

  private String checkbox(String prompt,
                          String name,
                          boolean isChecked) {
    String result =
      prompt + ": " +
      "<INPUT TYPE=\"CHECKBOX\" NAME=\"" + name + "\"";
    if (isChecked) {
      result = result + " CHECKED";
    }
    result = result + "><BR>\n";
    return(result);
  }

  private final String DOCTYPE =
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
    "Transitional//EN\">\n";
}


(3)配置好servlet后,演示效果:


完全填写好表单:



如果没有完全填写表单:


提交后的结果:(提示让填写missing的Data)

 

ok!





郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。