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.
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <%@ page contentType="text/html" %> <html> <body> <form name="myForm" method="post" action="functionsUsage.jsp"> Select an operation and click the button<br /><br /> <input type="radio" name="radioBtnGroup" value="uCaseOp" />Convert a string to upper Case<br /> <input type="radio" name="radioBtnGroup" value="lCaseOp" />Convert a string to lower Case<br /> <input type="radio" name="radioBtnGroup" value="subStrOp" />Get a substring of a string<br /> <input type="radio" name="radioBtnGroup" value="subStrAfterOp" />Get substring after<br /> <input type="radio" name="radioBtnGroup" value="subStrBeforeOp" />Get substring before<br /> <input type="radio" name="radioBtnGroup" value="trimOp" />Trim the string<br /> <input type="radio" name="radioBtnGroup" value="replaceOp" />Replaces characters in a string<br /> <input type="radio" name="radioBtnGroup" value="splitOp" />Split a string<br /> <input type="radio" name="radioBtnGroup" value="joinOp" />Join a string after it has been split<br /> <input type="radio" name="radioBtnGroup" value="escapeXml" />Escapes XML characters in a string<br /> <input type="radio" name="radioBtnGroup" value="indexOf" />Find start index of a substring in a string<br /> <input type="radio" name="radioBtnGroup" value="startsWithOp" />Check if the string starts with a particular substring<br /> <input type="radio" name="radioBtnGroup" value="endsWithOp" />Check if the string end with a particular substring<br /> <input type="radio" name="radioBtnGroup" value="containsOp" />Check if the string contains a particular substring(Case Sensitive)<br /> <input type="radio" name="radioBtnGroup" value="containsIgnoreCaseOp" />Check if the string contains a particular substring(Case In-sensitive)<br /> <input type="radio" name="radioBtnGroup" value="lengthOp" />Length of a String<br /> <br /> <input type="Submit" name="submitBtn" value="Click" /> </form> </body> </html> |
2) File Name : functionsUsage.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | <%@ 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
<br />
UpperCase : ${fn:toUpperCase('california')}
</c:when>
<c:when test="${param.radioBtnGroup=='lCaseOp'}">
Source String : CALIFORNIA
<br />
Lowercase : ${fn:toLowerCase('CALIFORNIA')}
</c:when>
<c:when test="${param.radioBtnGroup=='subStrOp'}">
Source String : California
<br />
Substring : ${fn:substring('California',4,7)}
</c:when>
<c:when test="${param.radioBtnGroup=='subStrAfterOp'}">
Source String : California
<br />
Substring After "Cali" : ${fn:substringAfter('California','Cali')}
</c:when>
<c:when test="${param.radioBtnGroup=='subStrBeforeOp'}">
Source String : California
<br />
Substring Before "fornia" : ${fn:substringBefore('California','fornia')}
</c:when>
<c:when test="${param.radioBtnGroup=='trimOp'}">
Source String : California
<br />
Trim the string " California " : ${fn:trim(' California ')}
</c:when>
<c:when test="${param.radioBtnGroup=='replaceOp'}">
Source String : California
<br />
Replace : ${fn:replace('California','a','A')}
</c:when>
<c:when test="${param.radioBtnGroup=='splitOp'}">
Source String : Cali;fornia
<br />
Split Separator : ";"
<br />
After Split an array is created with two items:
<br />
Item at index 0 : ${fn:split('Cali;fornia ',';')[0]}
<br />
Item at index 1 : ${fn:split('Cali;fornia ',';')[1]}
</c:when>
<c:when test="${param.radioBtnGroup=='joinOp'}">
Source String : Cali;fornia
<br />
Split Separator : ";"
<br />
Join Separator : "|"
<br />
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
<br />
Index Of "for" : ${fn:indexOf('California','for')}
<br />
</c:when>
<c:when test="${param.radioBtnGroup=='startsWithOp'}">
Source String : California
<br />
Starts With "Cal" : ${fn:startsWith('California','Cal')}
</c:when>
<c:when test="${param.radioBtnGroup=='endsWithOp'}">
Source String : California
<br />
Ends With "nia" : ${fn:endsWith('California','nia')}
</c:when>
<c:when test="${param.radioBtnGroup=='containsOp'}">
Source String : California
<br />
Contains "FOR" : ${fn:contains('California','FOR')}
</c:when>
<c:when test="${param.radioBtnGroup=='containsIgnoreCaseOp'}">
Source String : California
<br />
Contains IgnoreCase "FOR" : ${fn:containsIgnoreCase('California','FOR')}
</c:when>
<c:when test="${param.radioBtnGroup=='lengthOp'}">
Source String : California
<br />
Length of the string : ${fn:length('California')}
</c:when>
<c:otherwise>
Please make a selection to perform an operation
</c:otherwise>
</c:choose>
<br /><br />
<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
Comments
Related posts:






March 29, 2012 at 6:35 pm
I have been checking out a few of your stories and it’s nice stuff. I will definitely bookmark your blog.
March 31, 2012 at 10:58 am
I assume 1 of one’s ads triggered my browser to resize, you may need to place that in your blacklist.