Share on Google+Share on Google+

Use of Tag of JSTL

In this section, you will learn how to Tag of JSTL.

Use of <fn:endsWith(String, String)> Tag of JSTL


In this section we will learn how to use <fn:endsWith>  Tag of JSTL. This tag is used to check that given string ends with the specified sub string or not. This takes string type as arguments and return boolean type true or false.

Syntax :
boolean endsWith(java.lang.String, java.lang.String)


<%@ taglib uri="" prefix="fn" %>
<%@ taglib uri="" prefix="c" %>
        <title>Example of 'fn:endsWith' tag of jstl</title>
        <form method="POST">
            Here only .txt, .doc, .pdf are allowed.This application will check 
            <br>that you have entered file with valid extension or not.
            <table bgcolor="">     
                    <td>Enter file name with extension</td>
                    <td><input type="text" name="fName"></td>
                    <td><input type="submit" value="check"></td>
        <c:if test="${pageContext.request.method=='POST'}">    
            <c:set var="fname" value="${param.fName}"/>   

                <c:when test="${fn:endsWith(fname, '.txt')==true}">

                    <font size="5" color="green">Given file is text file.</font>
                                <c:when test="${fn:endsWith(fname, '.doc')==true}">

                    <font size="5" color="green">Given file is document file.</font>
                                <c:when test="${fn:endsWith(fname, '.pdf')==true}">

                    <font size="5" color="green">Given file is pdf file.</font>
                    <font size="5" color="red">Error found.</font>

This jsp code shows the following output :

If user enters file name with valid extension, output will be....

If found any error in search, output will be an error message.......

Download Source Code



Posted on: August 24, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.