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!

时间: 2024-10-12 23:35:41

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

怎么再jsp上显示一个表单

<%@ page import="java.io.*,java.util.*,java.sql.*"%> <% request.setCharacterEncoding("gb2312"); java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/GPSDatabase","root","

JSP怎么将表单提交到对应的servlet

昨天学习了这些内容,今天做一下分享吧,个人感觉挺乱的....呵呵,其实没事,慢慢就好了.难的不会,会的不难嘛!努力+认真就可以了,相信大家都可以的!加油!!! 下面的图是我用myeclipse建立的项目的目录. 目录结构有点多哈...没事其实很简单的! 下面是我的index.jsp中的全部代码:这里只是有一个表单,提交到TestServlet.java这个servlet中 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <%@

Servlet接口中的参数对象、JSP、EL表达式的内置对象罗列

Servlet接口参数对象: ServletRequest:service() 方法的参数 ServletResponse:service()方法的参数 ServletConfig:init()方法的参数 JSP九个内置对象: request                请求对象       类型javax.servlet.ServletRequest   作用域Request response              响应对象       类型javax.servlet.ServletRe

web工程缺失servlet/jsp/jstl依赖的解决方案

eclipse中新建maven web工程,会因为缺少servlet,jsp,jstl的依赖而报错,但如果直接依赖到pom.xml中后,会与tomcat中的相关jar包冲突,此时: <dependency> <groupId> jstl</groupId > <artifactId> jstl</artifactId > <version>${version}</ version> </dependency>

Jsp/Servlet根据请求参数自动填充Java对象:表单Bean

(1)问题的引出: 为了简单方便我们需要一种可以一次性全部提交表单数据的技术,也就是要说的:Bean表单 (2)BeanUtilities代码如下: package com.lc.ch04Biaodanshuju; import java.util.*; import javax.servlet.http.*; import org.apache.commons.beanutils.BeanUtils; public class BeanUtilities { public static voi

Servlet&amp;jsp基础:第五部分

声明:原创作品,转载时请注明文章来自SAP师太技术博客:www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将追究法律责任!原文链接:http://www.cnblogs.com/jiangzhengjun/p/4289323.html JSP. 80 JSP源码生成... 81 将JSP页面配置成Servlet 84 JSP基础语法... 84 JSP模板元素... 84 JSP表达式... 84 JSP脚本... 84 JSP声明... 85 EL

servlet&amp;jsp高级:第三部分

声明:原创作品,转载时请注明文章来自SAP师太技术博客:www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将追究法律责任!原文链接:http://www.cnblogs.com/jiangzhengjun/p/4289407.html EL表达式语言... 39 EL概述... 39 EL基本应用... 40 JSP标签的属性值中使用EL表达式... 40 JSP模板中使用EL表达式... 40 忽略JSP页面中的EL表达式... 40 忽略JSP页

javabean+servlet+jsp实现分页

前端实现用ligerUI实现分页,感觉用框架确实简单,闲着无聊,模拟着liger的分页界面实现了一遍(只要是功能,样式什么无视) 这里用基础的三层架构+servlet+jsp实现,思路很简单,把所有分页相关信息写入到一个pagebean类里面,service返回这个bean类,每次分页查询时都从该bean里查找信息.只是其中的细节问题比较繁琐,如边界处理(前端和后台边界都要处理),下拉框跳转后要显示当前页等等 这是ligerUI实现的分页样式(实现过程我的上一篇博客有写) 模拟实现过程: 目录结

servlet+jsp 增删改查

个人觉得这种模式是MVC模式 先写一个类,类里面有几个属性. 写一个接口.里面有增删改查的方法.                   (写在service里) 写一个类实现这个接口(实现里面的增删改查等操作)     (写在service里) 连接数据库 写 servlet操作,  处理增删改查的信息. Jsp页面  (写页面显示或者上传,添加的页面布局等)与servlet实现交互,从而实现以上功能. 增删改查具体实例如下————  具体代码: XinxManager.java package