Parse input from Form : Form Input « JSTL « Java Tutorial






<%@ 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>
  Download:  JSTLParseValueFromForm.zip( 1,225 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