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:
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