본문 바로가기

WEB_Programming/JSTL

14. JSTL Input 폼 처리

1. List All 폼 파라미터를 foreach를 이용하여 처리
1.1 index.jsp
<html>
  <head>
    <title>Set page parameters</title>
  </head>
  <body>
    This page allows you to enter information that is sent as request
    parameters to another page. The next page lists them. <P />

    <form action="listPageParameters.jsp" method="get">
      <table>
        <tr><td>Enter an adjective:</td>
            <td><input type="text" name="adjective" /></td>
        </tr>
        <tr><td>Enter a noun:</td>
            <td><input type="text" name="noun" /></td>
        </tr>
        <tr><td>Enter a color:</td>
            <td><input type="text" name="color" /></td>
        </tr>
      </table>
      <input type="submit" value="Send parameters" />
    </form>
  </body>
</html>


1.2 listPageParameters.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<html>
  <head>
    <title>List page parameters</title>
  </head>
  <body>
    You entered the following parameters:<br />
    <ol>
      <%-- 'param' is an implicit object. It is a Map that maps a 'key'
           (the parameter name) to a 'value' --%>
      <c:forEach var="pageParameter" items="${param}">
        <li> <c:out value="${pageParameter.key}" /> = <c:out value="${pageParameter.value}" />
      </c:forEach>
    </ol>
  </body>
</html>


2. 파라미터 변수 설정
2.1 index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<h1>Peter's Junk-Mail Service</h1>

<c:if test="${param.submitted}">
  <c:if test="${empty param.name}" var="noName" />
  <c:if test="${empty param.email}" var="noEmail" />
  <c:if test="${empty param.age}" var="noAge" />

  <c:catch var="ageError">
    <fmt:parseNumber var="parsedAge" value="${param.age}" />
    <c:if test="${parsedAge < 13}" var="youngAge" />
  </c:catch>
  <c:if test="${not empty ageError}" var="badAge" />

  <c:if
   test="${not (noName or noEmail or noAge or badAge or youngAge)}">
    <c:set value="${param.name}" var="name" scope="request"/>
    <c:set value="${param.email}" var="email" scope="request"/>
    <c:set value="${param.age}" var="age" scope="request"/>
    <jsp:forward page="spamFormHandler.jsp" />
  </c:if>
</c:if>

<form method="post">
  <input type="hidden" name="submitted" value="true" />

  <P>
  Enter your name:
  <input type="text" name="name"
    value="<c:out value="${param.name}"/>" />
  <br />
  <c:if test="${noName}">
   <small><font color="red">
     Note: you must enter a name
   </font></small>
  </c:if>
  </p>

  <P>
  Enter your email address:
  <input type="text" name="email"
    value="<c:out value="${param.email}"/>" />
  <br />
  <c:if test="${noEmail}">
   <small><font color="red">
     Note: you must enter an email address
   </font></small>
  </c:if>
  </p>

  <P>
  Enter your age:
  <input type="text" name="age" size="3"
    value="<c:out value="${param.age}"/>" />
  <br />
  <c:choose>
    <c:when test="${noAge}">
     <small><font color="red">
       Note: you must enter your age
     </font></small>
    </c:when>
    <c:when test="${badAge}">
     <small><font color="red">
       Note: I couldn't decipher the age you typed in
     </font></small>
    </c:when>
    <c:when test="${youngAge}">
     <small><font color="red">
       Note: You're too young to receive pornographic
       junk mail.  Please grow older and try again.
     </font></small>
    </c:when>
  </c:choose>
  </p>

  <input type="submit" value="Sign up" />

</form>


2.2 spamFormHandler.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<h4>
 <font face="arial">
  Excellent!  The information is now correct.  (We could save it in a
  database or process it further.)
 </font>
</h4>

3. Form으로 부터 Date 값 획득
3.1 index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<form method="post" action="dateHandler.jsp">

  Please enter your birthday:

  <select name="month">
    <option value="Jan">January</option>
    <option value="Feb">February</option>
    <option value="Mar">March</option>
    <option value="Apr">April</option>
    <option value="May">May</option>
    <option value="Jun">June</option>
    <option value="Jul">July</option>
    <option value="Aug">August</option>
    <option value="Sep">September</option>
    <option value="Oct">October</option>
    <option value="Nov">November</option>
    <option value="Dec">December</option>
  </select>

  <select name="day">
    <c:forEach begin="1" end="31" var="day">
      <option><c:out value="${day}"/></option>
    </c:forEach>
  </select>

  <select name="year">
    <c:forEach begin="1930" end="2003" var="year">
      <option><c:out value="${year}"/></option>
    </c:forEach>
  </select>

  <input type="submit" value="Submit" />

</form>


3.2 dateHandler.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<fmt:parseDate
  var="date"
  parseLocale="en_US"
  value="${param.month} ${param.day}, ${param.year}"/>

You were born
<fmt:formatDate
  value="${date}"
  dateStyle="full"/>.


4. Form으로 부터 input 파싱
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Parse Number</title>
  </head>

  <body>
    <form method="POST">
      <table>
        <tr>
          <td width="100%" colspan="2">
               Number Formatting
          </td>
        </tr>

        <tr>
          <td width="47%">Enter a number to be parsed:</td>

          <td width="53%">
            <input type="text" name="num" size="20" />
          </td>
        </tr>

        <tr>
          <td width="100%" colspan="2">
            <p align="center">
              <input type="submit" value="Submit" name="submit" />

              <input type="reset" value="Reset" name="reset" />
            </p>
          </td>
        </tr>
      </table>

      <P>&#160;</p>
    </form>

    <c:if test="${pageContext.request.method=='POST'}">
      <table>
        <tr>
          <td width="100%" colspan="2">
             Formatting: <c:out value="${param.num}" escapeXml="false" />
          </td>
        </tr>

        <tr>
          <td width="51%">type="number"</td>
          <td width="49%">
            <c:catch var="e">
              <fmt:parseNumber var="i" type="number" value="${param.num}" />

              <c:out value="${i}"  escapeXml="false" />
            </c:catch>

            <c:out value="${e}"  escapeXml="false" />
          </td>
        </tr>

        <tr>
          <td width="51%">type="currency"</td>
          <td width="49%">
            <c:catch var="e">
              <fmt:parseNumber var="i" type="currency" value="${param.num}" />
              <c:out value="${i}"  escapeXml="false" />
            </c:catch>

            <c:out value="${e}"  escapeXml="false" />
          </td>
        </tr>

        <tr>
          <td width="51%">type="percent"</td>

          <td width="49%">
            <c:catch var="e">
              <fmt:parseNumber var="i" type="percent"
              value="${param.num}" />

              <c:out value="${i}"  escapeXml="false" />
            </c:catch>

            <c:out value="${e}"  escapeXml="false" />
          </td>
        </tr>

        <tr>
          <td width="51%">type="number" integerOnly="true"</td>

          <td width="49%">
            <c:catch var="e">
              <fmt:parseNumber var="i" integerOnly="true"
              type="number" value="${param.num}" />

              <c:out value="${i}"  escapeXml="false" />
            </c:catch>

            <c:out value="${e}"  escapeXml="false" />
          </td>
        </tr>
      </table>
    </c:if>
  </body>
</html>



5. 폼 에러 체크와 Forward 수행
5.1 Bid.java
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;
    }
   
}


5.2 Bidder.java
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;
    }
   
}


5.3 BidError.java
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;
    }
   
}


3.4 enterBid.jsp
<%@ 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>


3.5 showBid.jsp
<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>


3.6 bid 처리 jsp
<%@ 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>

6. Form Input폼으로 부터 URL생성하기
6.1 index.jsp
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<c:set var="originalURL" value="http://localhost:8080/chapter05/core/url/url.jsp" />

<html>
  <head>
    <title>the c:url action </title>
  </head>
  <body>
    This page takes 3 values that you specify, and forwards them to another JSP.
    That JSP will create a URL to another page, that then extracts the
    parameters and displays them.
    <p />
      <form action="createURL.jsp" method="post">
        <table>
          <tr><td>Enter name:</td>
              <td><input type="text" name="name"   /></td></tr>
          <tr><td>Enter age:</td>
              <td><input type="text" name="age"    /></td></tr>
          <tr><td>Enter gender:</td >
              <td><input type="text" name="gender" /></td></tr>
        </table>
        <input type="submit" value="Submit details" />
      </form>
  </body>
</html>


6.2 list처리
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<html>
  <head>
    <title>the c:url action (3)</title>
  </head>
  <body>
    <h3>List of query string parameters:</h3>
    <ul>
      <c:forEach items="${param}" var="currentParam">
        <li><c:out value="${currentParam.key}" />
            = <c:out value="${currentParam.value}" /></li>
      </c:forEach>
    </ul>
 </body>
</html
>

6.3 결과 표시
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<c:url value="displayValues.jsp" var="displayURL">
  <c:param name="nameParam"   value="${param.name}" />
  <c:param name="ageParam"    value="${param.age}" />
  <c:param name="genderParam" value="${param.gender}" />
</c:url>

<html>
  <head>
    <title>the c:url action (2)</title>
  </head>
  <body>
    This page receives the values you specified, and creates a URL that contains
    them.
    <p />
    The generated URL is <c:out value="${displayURL}" />. <p/>
    Click <a href='<c:out value="${displayURL}" />'>here</a> to view the it.
  </body>
</html>

7. 파라미터 변수와 출력 에러 메시지 체크
7.1
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<h1>Peter's Junk-Mail Service</h1>

<c:if test="${param.submitted}">
  <c:if test="${empty param.name}" var="noName" />
  <c:if test="${empty param.email}" var="noEmail" />
  <c:if test="${empty param.age}" var="noAge" />

  <c:catch var="ageError">
    <fmt:parseNumber var="parsedAge" value="${param.age}" />
    <c:if test="${parsedAge < 13}" var="youngAge" />
  </c:catch>
  <c:if test="${not empty ageError}" var="badAge" />

  <c:if
   test="${not (noName or noEmail or noAge or badAge or youngAge)}">
    <c:set value="${param.name}" var="name" scope="request"/>
    <c:set value="${param.email}" var="email" scope="request"/>
    <c:set value="${param.age}" var="age" scope="request"/>
    <jsp:forward page="spamFormHandler.jsp" />
  </c:if>
</c:if>

<form method="post">
  <input type="hidden" name="submitted" value="true" />

  Enter your name:
  <input type="text" name="name"
    value="<c:out value="${param.name}"/>" />
  <br />
  <c:if test="${noName}">
   <small><font color="red">
     Note: you must enter a name
   </font></small>
  </c:if>
  </p>

  Enter your email address:
  <input type="text" name="email"
    value="<c:out value="${param.email}"/>" />
  <br />
  <c:if test="${noEmail}">
   <small><font color="red">
     Note: you must enter an email address
   </font></small>
  </c:if>
  </p>


  Enter your age:
  <input type="text" name="age" size="3"
    value="<c:out value="${param.age}"/>" />
  <br />
  <c:choose>
    <c:when test="${noAge}">
     <small><font color="red">
       Note: you must enter your age
     </font></small>
    </c:when>
    <c:when test="${badAge}">
     <small><font color="red">
       Note: I couldn't decipher the age you typed in
     </font></small>
    </c:when>
    <c:when test="${youngAge}">
     <small><font color="red">
       Please grow older and try again.
     </font></small>
    </c:when>
  </c:choose>
  </p>

  <input type="submit" value="Sign up" />

</form>


7.2
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<h4>
 <font face="arial">
  Excellent!  The information is now correct.  (We could save it in a
  database or process it further.)
 </font>
</h4>

'WEB_Programming > JSTL' 카테고리의 다른 글

16. JSTL Form TextField  (0) 2008.06.24
15. JSTL 폼 Select 예제  (0) 2008.06.24
13. JSTL CheckBox 폼 처리  (0) 2008.06.20
12. JSTL 예외 처리  (0) 2008.06.20
11. JSTL Cookie 처리  (0) 2008.06.20