Servlet/Jsp implementation when the parameter is indeed or missing, re-display the input form, servletjsp

Source: Internet
Author: User

Servlet/Jsp implementation when the parameter is indeed or missing, re-display the input form, servletjsp

(1) In some cases, our forms need to be filled out by the user, but they are submitted without being filled in. We need to keep the data that has been filled in and then let the user fill in the data that has not been filled in.

(2) Implement functions with a servlet that processes the auction bidding:

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 = S ErvletUtilities. 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 se TEmailAddress (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;}/** determine whether to complete */public boolean I SComplete () {return (hasValue (getItemID () & hasValue (getItemName () & hasValue (getBidderName () & hasValue (getEmailAddress ()) & (getBidPrice ()> 0);} // determines whether a public boolean isPartlyComplete () {boolean flag = (hasValue (getItemID () is missing ()) | hasValue (getItemName () | hasValue (getBidderName () | hasValue (getEmailAddress () | (getBidPrice ()> 0) | isAutoIncrement ()); return (flag);} privat E 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 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) After configuring the servlet, demonstrate the effect:


Complete the form:



If the form is not fully filled:


Result After submission: (Prompt for missing Data)

 

OK!






How does a servlet obtain parameters and display them on the jsp page?

Retrieve parameters in servlet and save them in request
String param = request. getParameter ("param"); // retrieve the parameter value
Request. setAttribute ("param", param); // save it to the request
Obtain the value based on param on the page.

How does servlet receive jsp page data?

Yes.
Method 1: URL with parameters, for example, through www.abc.com/..age%16.pdf
String name = request. getParameter ("name");
String age = request. getParameter ("age.
Method 2: Use <% request. setAttribute ("name", "zhangsan"); %>. Use request in the background. getAttribute ("name"); you can get "Zhang San ".
Forms are the most convenient.

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.