
- Basic JSP Tutorial
- JSP - Home
- JSP - Overview
- JSP - Environment Setup
- JSP - Architecture
- JSP - Lifecycle
- JSP - Syntax
- JSP - Directives
- JSP - Actions
- JSP - Implicit Objects
- JSP - Client Request
- JSP - Server Response
- JSP - Http Status Codes
- JSP - Form Processing
- JSP - Writing Filters
- JSP - Cookies Handling
- JSP - Session Tracking
- JSP - File Uploading
- JSP - Handling Date
- JSP - Page Redirect
- JSP - Hits Counter
- JSP - Auto Refresh
- JSP - Sending Email
- Advanced JSP Tutorials
- JSP - Standard Tag Library
- JSP - Database Access
- JSP - XML Data
- JSP - Java Beans
- JSP - Custom Tags
- JSP - Expression Language
- JSP - Exception Handling
- JSP - Debugging
- JSP - Security
- JSP - Internationalization
- JSP Useful Resources
- JSP - Questions and Answers
- JSP - Quick Guide
- JSP - Useful Resources
- JSP - Discussion
JSTL - Core <c:forEach>, <c:forTokens> Tag
These tags exist as a good alternative to embedding a Java for, while, or do-while loop via a scriptlet. The <c:forEach> tag is a commonly used tag because it iterates over a collection of objects. The <c:forTokens> tag is used to break a string into tokens and iterate through each of the tokens.
Attribute
The <c:forEach> tag has the following attributes −
Attribute | Description | Required | Default |
---|---|---|---|
items | Information to loop over | No | None |
begin | Element to start with (0 = first item, 1 = second item, ...) | No | 0 |
end | Element to end with (0 = first item, 1 = second item, ...) | No | Last element |
step | Process every step items | No | 1 |
var | Name of the variable to expose the current item | No | None |
varStatus | Name of the variable to expose the loop status | No | None |
The <c:forTokens> tag has similar attributes as that of the <c:forEach> tag except one additional attribute delims which specifies sharacters to use as delimiters.
Attribute | Description | Required | Default |
---|---|---|---|
delims | Characters to use as delimiters | Yes | None |
Example for <c:forEach>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> <html> <head> <title><c:forEach> Tag Example</title> </head> <body> <c:forEach var = "i" begin = "1" end = "5"> Item <c:out value = "${i}"/><p> </c:forEach> </body> </html>
The above code will generate the following result −
Item 1 Item 2 Item 3 Item 4 Item 5
Example for <c:forTokens>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> <html> <head> <title><c:forTokens> Tag Example</title> </head> <body> <c:forTokens items = "Zara,nuha,roshy" delims = "," var = "name"> <c:out value = "${name}"/><p> </c:forTokens> </body> </html>
The above code will generate the following result −
Zara nuha roshy
jsp_standard_tag_library.htm
Advertisements