JSTL Form Error Check and Forward : Form Input « JSTL « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » JSTL » Form Input 
24.15.5.JSTL Form Error Check and Forward
package beans;
public class Bid {
    
    /** Holds value of property price. */
    private long price;
    
    /** Holds value of property item. */
    private String item;
    
    /** Creates a new instance of Bid */
    public Bid() {
    }
    
    /** Getter for property price.
     @return Value of property price.
     *
     */
    public long getPrice() {
        return this.price;
    }
    
    /** Setter for property price.
     @param price New value of property price.
     *
     */
    public void setPrice(long price) {
        this.price = price;
    }
    
    /** Getter for property item.
     @return Value of property item.
     *
     */
    public String getItem() {
        return this.item;
    }
    
    /** Setter for property item.
     @param item New value of property item.
     *
     */
    public void setItem(String item) {
        this.item = item;
    }
    
}
package beans;

public class Bidder {
    
    /** Holds value of property item. */
    private String item;
    
    /** Holds value of property price. */
    private long price;
    
    /** Holds value of property result. */
    private String result;
    
    /** Creates a new instance of Bidder */
    public Bidder() {
    }
    
    /** Getter for property item.
     @return Value of property item.
     *
     */
    public String getItem() {
        return this.item;
    }
    
    /** Setter for property item.
     @param item New value of property item.
     *
     */
    public void setItem(String item) {
        this.item = item;
    }
    
    /** Getter for property price.
     @return Value of property price.
     *
     */
    public long getPrice() {
        return this.price;
    }
    
    /** Setter for property price.
     @param price New value of property price.
     *
     */
    public void setPrice(long price) {
        this.price = price;
    }
    
    /** Getter for property result.
     @return Value of property result.
     *
     */
    public String getResult() {
        /* simulate bid result */
        this.result = "Sorry your bid did not win. The successful bidder was joe1233.";
        return this.result;
    }
    
}
package beans;

public class BidError {
    
    /** Holds value of property msg. */
    private String msg;
    
    /** Creates a new instance of BidError */
    public BidError() {
    }
    
    /** Getter for property msg.
     @return Value of property msg.
     *
     */
    public String getMsg() {
        return this.msg;
    }
    
    /** Setter for property msg.
     @param msg New value of property msg.
     *
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }
    
}
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
<title>Enter Your Bid</title></head>
<body>
<table class="mainBox" width="400">
<tr><td class="boxTitle" colspan="2">
Wrox JSP Auction
</td></tr>
<c:if test="${!(empty biderror)}">
  <tr>
    <td class="errorText" colspan="2">
       ${biderror.msg}
    </td>
  </tr>
</c:if>

<tr><td colspan="2">&nbsp;</td></tr>
<tr><td>
<form  action="action.jsp" method="get">
<table>
<tr>
<td width="200">Item to bid on</td><td>
<select name="item">
<option>27 inch TV</option>
<option>DVD Player</option>
<option>Digital Camera</option>
</select>
<input type="hidden" name="action" value="bid"/>
</td></tr>
<tr>
<td>Bid Price:</td>
<td><input name="price" type="text" width="10"/>
</td></tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr><td colspan="2" align="center">
<input type="submit" value="Bid now!"/>
</td></tr>
</table>
</form>
</td></tr></table>
</body>
</html>
<html>
<head>
<title>Show the Bid</title></head>
<body>
<table class="mainBox" width="600">
<tr><td class="boxTitle" colspan="2">
Your Auction Bid
</td></tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr><td class="tableLabel" width="30%">
  Item bid for
</td>
<td class="tableCell">
<c:out value="${bidinfo.item}"></td>
</tr>
<tr><td class="tableLabel">
  Bid price
</td>
<td class="tableCell">
<c:out value="${bidinfo.price}"/></td>
</tr>
<tr><td class="tableLabel">
  Bid result
</td>
<td class="resultCell">
<c:out value="${bidresult}"/></td>
</tr>


</table>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:choose>
  <c:when test="${empty param.action}">
     <jsp:forward page="enterbid.jsp"/>
  </c:when>
  <c:when test="${param.action eq 'bid'}">
   <!--  validation code -->
   <c:if test="${(param.price <= 0) ||  (param.price >= 999)}">
       <jsp:useBean id="biderror" class="beans.BidError" scope="request">
        <jsp:setProperty name="biderror" property="msg" value="Sorry, your bid is not in range. Please enter again."/>
     </jsp:useBean>
     <jsp:forward page="index.jsp"/>
  </c:if>
  <!-- data validated -->
       <jsp:useBean id="bidinfo" class="beans.Bid" scope="request">
        <jsp:setProperty name="bidinfo" property="*"/> 
     </jsp:useBean>

  <!-- perform bidding -->
      <jsp:useBean id="bidder" class="beans.Bidder" scope="request">
        <jsp:setProperty name="bidder" property="item"/>
        <jsp:setProperty name="bidder" property="price"/>
     </jsp:useBean>
     <c:set var="bidresult" value="${bidder.result}" scope="request"/>


     <jsp:forward page="showbid.jsp"/>   
</c:when>
</c:choose>
  Download:  JSTLFormErrorCheckAndForward.zip( 1,071 k)
24.15.Form Input
24.15.1.Use ForEach to List All Form Parameters
24.15.2.Set Parameter Value
24.15.3.Get Date value from Form
24.15.4.Parse input from Form
24.15.5.JSTL Form Error Check and Forward
24.15.6.Use JSTL to Create URL From Form Input
24.15.7.Check Parameter Value and Output Error Message
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.