JSTL fn length Example

JSTL fn length Example


Posted in : Java Posted on : April 11, 2012 at 7:09 PM Comments : [ 0 ]

In this tutorial you will learn about the JSTL fn length function.

JSTL fn length Example

In this tutorial you will learn about the JSTL fn length function.

length function, however the size() method is defined by the java.util.Collection interface can not be accessed through the JSP EL because the JavaBeans component design pattern is not confirmed by it for the properties, is used for finding out the how many characters (including the white space) are there in a string or how many items are there in a collection.

Syntax :

int length(java.lang.Object)

Example :

Here an example is being given below will demonstrate you about the JSTL fn length function. In this example I will try for finding the number of items from the collection and the number of characters from a string. For finding the number of characters of a string I have created a simple JSP page where I have hard coded the string value into a variable using <c:set var="" value=""> tag of JSTL core tag library. Then used the JSTL function fn:length() to find the character of a string. Next to find the number of items in collection I have created a collection and added the items into it and then set this items value into a request using request.setAttribute() and used this attribute value in the <c:forEach> tag to iterate over the items and finally to found the number of items in a collection I used the fn:length() function.

JstlFnLength.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.Collection" %>
<%@ page import="java.util.LinkedList" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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=ISO-8859-1">
<title>JSTL fn:length() Example</title>
</head>
<body>
<h3>Finding the Number of Characters in a String...</h3>
<c:set var="str" value="Devmanuals"/>
<p>Number of characters in <b>${str}</b> are : ${fn:length(str)}</p>
<h3>Finding the Number of items in a Collection...</h3>
<%
Collection<String> col = new LinkedList<String>();
col.add("Rose");
col.add("Maria");
col.add("Dev");
col.add("Manuals");
request.setAttribute("collection", col);
%>
<p>Items in a Collection are ...</p>
<c:forEach var="i" items="${collection }">
${i}<br>
</c:forEach>
<p>Number of Items in a Collection are : ${fn:length(collection)}</p>

</body>
</html>

How to run this example

Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :

  • jstl.jar
  • standard.jar

After adding of these jar files you may execute your program in the following ways :

  • Select JstlFnLength.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlFnLength.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
  • A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish

NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.

Output :

When the execution process will be completed successfully you will get the output on your eclipse browser as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics