Now, let’s have a look at the JSTL Function tag. Create a new JSP. Let’s name it Jstl_Functions_Tags.jsp. Then create a new link to direct to this new JSP.
<table border="1">
<tr>
<td>
<a href="Jstl_Functions_Tags.jsp">JSTL Functions Tags</a>
</td>
<td>
String Manipulations
</td>
</tr>
</table>
Do not forget to include JSTL taglib directive for prefix fn in our JSP. Here is how to import the fn JSTL taglib directive.
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
The JSTL functions tag will mainly deal with string manipulation. Let’s try some of them.
<c:set var="sentence" value=" Try This String for Manipulations " />
<c:out value="${sentence}" />
Above codes are used to set a fixed string to variable “sentence” and print it out for display. Next, please pay attention on how we are going to combine JSTL core <c> tag with JSTL function <fn> tag.
<c:out value="${fn:length(sentence)}" />
Above codes will print out the length of variable “sentence”. It will produce 35 since all characters are counted.
<c:out value="${fn:toUpperCase(sentence)}" />
<c:out value="${fn:toLowerCase(sentence)}" />
Above codes are used to convert our “sentence” to UPPER or LOWER case.
<c:out value="${fn:trim(sentence)}" />
<c:out value="${fn:length(fn:trim(sentence))}" />
<c:out value="${fn:replace(sentence,' ','_')}" />
Above codes are used to trim the “sentence” and count the length of trimmed “sentence”. Trimming the sentence means that we are removing the extra spaces (if any) in the string either in front or in back. You need to know that the spaces within the string will NOT be trimmed. It should produce 33 since the spaces at the beginning and at the end will be discarded. While the sample above of fn:replace will replace all occurrences of spaces with underscores.
<c:out value="${fn:substring(sentence,0,8)}" />
<c:out value="${fn:substringAfter(sentence,'for')}" />
<c:out value="${fn:substringBefore(sentence,'for')}" />
<c:out value="${fn:indexOf(sentence,'g')}" />
The fn:substring will take 3 parameters. 1st parameter is our “sentence” variable itself. 2nd parameter is for the starting point and 3rd parameter is where it must stop to return result of substring. More advanced substring functions are fn:substringAfter and fn:substringBefore. See at the screenshot to see how they work.
And the fn:indexOf will return the position of defined character in our “sentence”.
<c:out value="${fn:contains(sentence,'G')}" />
<c:out value="${fn:containsIgnoreCase(sentence,'G')}" />
<c:out value="${fn:startsWith(sentence,' Try')}" />
<c:out value="${fn:endsWith(sentence,'pulations ')}" />
fn:contains will return boolean value true if correct and false otherwise. However, it is case sensitive and will sensitively check for the lower case and upper case of the string. fn:containsIgnoreCase work in the same way as fn:contains but it is not case sensitive which means that it will not care whether it is in lower case or in upper case. fn:startsWith and fn:endsWith will also return boolean to check whether the “sentence” starts or ends with corresponding given value.
Again, clean build the project and run the project. The index.jsp will appear as usual and click the “Jstl_Functions_Tags” to see the result.
6. Conclusion
Now we have known what JSTL is and how to use it. Use JSTL anytime when possible. JSTL could empower our web app. From what we learnt, we could think of when or in what conditions JSTL could be used for. Some examples of use of JSTL are
display format and internationalization print out values from servlets response-request or even database values as in work with POJO orjava bean validating users’ input on UI parsing users’ input value URL handling
You could think of other several examples and extend