SERVLET/JSP implementation when parameters are true or missing re-display the input form

Source: Internet
Author: User

(1) In some cases, we need to fill out the form, but did not fill in the submission, we need to keep the completed data and then let the user fill in the non-filled data

(2) To implement the function with a servlet that deals with auction bids:

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);  The public void Setautoincrement (Boolean autoincrement) {this.autoincrement = AutoIncrement; }/** to determine whether to complete/public boolean iscomplete () {return (HasValue (Getitemid ()) && HasValue (Getitemname ( )) && HasValue (Getbiddername ()) && HasValue (getemailaddress ()) && (g  Etbidprice () > 0));           }//determine if there are no entries filled in 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 ("")); }}


The Bidservlet code is as follows:

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 is 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, BidinfoBid) throws Servletexception, IOException {submitbid (BID);    Response.setcontenttype ("text/html");    PrintWriter out = Response.getwriter ();    String title = "Bid submitted"; Out.println (DOCTYPE + "


(3) After configuring the servlet, demonstrate the effect:


Fill out the form completely:



If the form is not fully populated:


Results after submission: (Prompt to fill in missing data)

Ok!





SERVLET/JSP implementation when parameters are true or missing re-display the input form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.