Jstl Fortokens Tag Example - Split Upwardly String Inward Jsp

Advertisement

Masukkan script iklan 970x90px

Jstl Fortokens Tag Example - Split Upwardly String Inward Jsp

Sabtu, 18 Juli 2020

JSTL forTokens tag is roughly other tag inwards marrow JSTL library to back upward Iteration or looping. It effectively complements, to a greater extent than useful <c:forEach> tag, past times allowing you lot to iterate over comma separated or whatever delimited String. You tin operate this tag to carve upward string inwards JSP as well as tin operate on them individually. forTokens tag has similar attribute similar forEach JSTL tag except ane to a greater extent than attribute called delims, which specifies delimiter. For instance to iterate over colon separated String "abc:cde:fgh:ijk", delims=":". By the way, forTokens tag likewise bring multiple delimiter, which means, you lot tin split a big string into token based upon multiple delimiter e.g. colon(:) as well as pipe (|), This volition move to a greater extent than clear, when nosotros volition meet examples of JSTL forTokens tag inwards JSP. Rest of attribute e.g. items, var, varStatus, begin, end as well as step are same, equally they are inwards instance of <c:forEach> tag. For quick review, items specify String which needs to move split-ed inwards token as well as var gibe electrical flow String.


JSTL <c:forTokens> Tag Example

JSTL forTokens tag is roughly other tag inwards marrow JSTL library to back upward Iteration or looping JSTL forTokens Tag Example - Split String inwards JSPHere is our consummate code instance of using JSTL forTokens tag inwards JSP page. In this example, nosotros get-go carve upward a comma separate String using forTokens tag past times specifying delims=";". When nosotros iterate over tokens, var stand upward for electrical flow token. In the minute example, nosotros stimulate got specified multiple delimiter inwards delims attribute, delims="|," to carve upward String past times pipe(|) grapheme as well as comma (,) character.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title> JSTL forTokens tag Example - Iterate over comma separated String inwards JSP</title>
    </head>

    <body>

        <h3> forTokens Tag Example</h3>

        <h4>String amongst comma ',' delimiter</h4>

        <c:forTokens var="token" items="Java, J2EE, JSTL, JSP, Servlet"
                     delims=",">
            <c:out value="${token}"/> </br>
        </c:forTokens>

        <h4>Iterating over String amongst '|' as well as ',' delimiters</h4>

        <c:forTokens var="token" items="USA,New York,UK|London,Japan|Tokyo"
                     delims="|,">
            <c:out value="${token}"/> </br>
        </c:forTokens>
    </body>
</html>

Output:
forTokens Tag Example
String amongst comma ',' delimiter
Java
J2EE
JSTL
JSP
Servlet

Iterating over String amongst '|' as well as ',' delimiters
USA
New York
UK
London
Japan
Tokyo

That's all on How to operate JSTL forTokens tag for iterating over comma separated String. Good affair most forTokens tag is that, it non solely complements forEach tag, merely likewise back upward multiple delimiters for breaking String into tokens. Quite handy to procedure asking parameters as well as other text data.

Further Learning
Spring Framework 5: Beginner to Guru
Java Web Fundamentals By Kevin Jones
JSP, Servlets as well as JDBC for Beginners: Build a Database App