JSTL ForTokens : ForTokens « JSTL « Java Tutorial






<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Updatable Collections</title>
  </head>

  <body>
    <table border="0">
      <form method="post">
        <tr><td>Parse for Tokens</td></tr>
        <tr>
          <td valign="top">
            <p align="left">Enter a sentence:
            <br />
            <input width="20" maxwidth="20" name="text" size="50" />
            <br />&#160;</p>
          </td>
        </tr>

        <tr>
          <td valign="top">
            <p align="center">
              <input type="submit" name="parse" value="Parse" />
            </p>
          </td>
        </tr>
      </form>
    </table>

    <c:if test="${pageContext.request.method=='POST'}">
      <table border="1">
        <c:set var="i" value="1" />
        <c:forTokens items="${param.text}" var="word" delims=" ,.?!">
          <c:set var="i" value="${i}" />

          <tr>
            <td>
              <b>Word 
              <c:out value="${i}" />
              </b>
            </td>

            <td>
              <c:out value="${word}" />
            </td>
          </tr>
        </c:forTokens>
      </table>
    </c:if>
  </body>
</html>
  Download:  JSTLForToken.zip( 1,225 k)








24.6.ForTokens
24.6.1.Use JSTL ForTokens to split string
24.6.2.JSTL ForTokens