java left logo
java middle logo
java right logo
 

Home arrow Java Tutorials arrow Introduction to JSTL using NetBeans
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4100
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Introduction to JSTL using NetBeans E-mail
User Rating: / 163
PoorBest 

5. Implementation of JSTL Function Tags

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.

 

Image

 

Image

 

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

  1. display format and internationalization
  2. print out values from servlets response-request or even database values as in work with POJO or java bean
  3. validating users’ input on UI
  4. parsing users’ input value
  5. URL handling

You could think of other several examples and extend your knowledge on JSTL. And apply it on your project.

 Related Tips



 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.