• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JSTL Function in JSP 2.0

March 3, 2009 //  by Krishna Srinivasan//  Leave a Comment

In JSP 2.0 we can perform string operations in JSP without using any java code inside scriptlets. This is possible with the latest release of JSTL 1.1. The new library called Functions with prefix as fn allows us to perform string operations in JSP 2.0.

also read:

  • Java EE Tutorials
  • Servlets Interview Questions
  • New Features in Servlets 3.0
  • Asynchronous Servlets in Servlets 3

The following example explains all the string operation which we can perform with the new fn tags.
Note:

  • You will need the following files to run this example :
  • jstl.jar and standard.jar inside /WEB-INF/lib
  • fn.tld, c.tld inside any location under any lcation under WEB-INF
  • JSP 2.0 Function : Example

    1) File Name : firstPage.jsp

    <%@ page contentType="text/html" %>
    <html>
    <body>
    	<form name="myForm" method="post" action="functionsUsage.jsp">
    		Select an operation and click the button
    		<input type="radio" name="radioBtnGroup" value="uCaseOp" />Convert a string to upper Case
    		<input type="radio" name="radioBtnGroup" value="lCaseOp" />Convert a string to lower Case
    		<input type="radio" name="radioBtnGroup" value="subStrOp" />Get a substring of a string
    		<input type="radio" name="radioBtnGroup" value="subStrAfterOp" />Get substring after
    		<input type="radio" name="radioBtnGroup" value="subStrBeforeOp" />Get substring before
    		<input type="radio" name="radioBtnGroup" value="trimOp" />Trim the string
    		<input type="radio" name="radioBtnGroup" value="replaceOp" />Replaces characters in a string
    		<input type="radio" name="radioBtnGroup" value="splitOp" />Split a string
    		<input type="radio" name="radioBtnGroup" value="joinOp" />Join a string after it has been split
    		<input type="radio" name="radioBtnGroup" value="escapeXml" />Escapes XML characters in a string
    		<input type="radio" name="radioBtnGroup" value="indexOf" />Find start index of a substring in a string
    		<input type="radio" name="radioBtnGroup" value="startsWithOp" />Check if the string starts with a particular substring
    		<input type="radio" name="radioBtnGroup" value="endsWithOp" />Check if the string end with a particular substring
    		<input type="radio" name="radioBtnGroup" value="containsOp" />Check if the string contains a particular substring(Case Sensitive)
    		<input type="radio" name="radioBtnGroup" value="containsIgnoreCaseOp" />Check if the string contains a particular substring(Case In-sensitive)
    		<input type="radio" name="radioBtnGroup" value="lengthOp" />Length of a String
    		
    		<input type="Submit" name="submitBtn" value="Click" />
    	</form>
    </body>
    </html>
    

    2) File Name : functionsUsage.jsp

    <%@ page contentType="text/html" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    <html>
    	<body>
    	<c:choose>
    		<c:when test="${param.radioBtnGroup=='uCaseOp'}">
    			Source String : california
    			
    			UpperCase	  : ${fn:toUpperCase('california')}
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='lCaseOp'}">
    			Source String : CALIFORNIA
    			
    			Lowercase	  : ${fn:toLowerCase('CALIFORNIA')}
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='subStrOp'}">
    			Source String : California
    			
    			Substring	  : ${fn:substring('California',4,7)}
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='subStrAfterOp'}">
    			Source String : California
    			
    			Substring After	"Cali" : ${fn:substringAfter('California','Cali')}
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='subStrBeforeOp'}">
    			Source String : California
    			
    			Substring Before "fornia" : ${fn:substringBefore('California','fornia')}
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='trimOp'}">
    			Source String : California
    			
    			Trim the string " California " : ${fn:trim(' California ')}
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='replaceOp'}">
    			Source String : California
    			
    			Replace : ${fn:replace('California','a','A')}
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='splitOp'}">
    			Source String : Cali;fornia
    			
    			Split Separator : ";"
    			
    			After Split an array is created with two items:
    			
    			Item at index 0 : ${fn:split('Cali;fornia ',';')[0]}
    			
    			Item at index 1 : ${fn:split('Cali;fornia ',';')[1]}
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='joinOp'}">
    			Source String : Cali;fornia
    			
    			Split Separator : ";"
    			
    			Join Separator : "|"
    			
    			Join :
    			<c:set var="a1" value="${fn:split('Cali;fornia', ';')}"  />
    			<c:out value="${fn:join(a1, '|')}" />
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='escapeXml'}">
    			${fn:escapeXml('<city>California</city>')}
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='indexOf'}">
    			Source String : California
    			
    			Index Of "for" : ${fn:indexOf('California','for')}
    			
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='startsWithOp'}">
    			Source String : California
    			
    			Starts With "Cal" : ${fn:startsWith('California','Cal')}
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='endsWithOp'}">
    			Source String : California
    			
    			Ends With "nia" : ${fn:endsWith('California','nia')}
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='containsOp'}">
    			Source String : California
    			
    			Contains "FOR" : ${fn:contains('California','FOR')}
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='containsIgnoreCaseOp'}">
    			Source String : California
    			
    			Contains IgnoreCase "FOR" : ${fn:containsIgnoreCase('California','FOR')}
    		</c:when>
    		<c:when test="${param.radioBtnGroup=='lengthOp'}">
    			Source String : California
    			
    			Length of the string : ${fn:length('California')}
    		</c:when>
    		<c:otherwise>
    			Please make a selection to perform an operation
    		</c:otherwise>
    	</c:choose>
    
    	
    	<a href="home.jsp"><< Back</a>
    	</body>
    </html>
    

    3) Output

    Source String : Cali;fornia 
    Split Separator : ";" 
    Join Separator : "|" 
    Join : Cali|fornia 
    << Back
    

    JSP 2.0 Function : Explanation

    In this example we can see the usage of the string functions defined as part of the new JSTL 1.1 tag library.
    The functions are part of the new Function library and they needs to be prefixed with fn as we can see in the examples. This reduces lot of development effort and our jsps look a lot cleaner as we dont have to mix java code inside jsp.

    The Functions library includes the following string functions : toUpperCase, toLowerCase, substring, substringAfter, substringBefore, trim, replace, split, join, indexOf, startsWith, endsWith, contains, containsIgnoreCase and length

Category: Java EETag: JSP 2.0, JSTL

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « ServletContextListener Example
Next Post: XQuery Syntax »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact