JSTL fn endsWith Example

JSTL fn endsWith Example


Posted in : Java Posted on : April 13, 2012 at 6:51 PM Comments : [ 0 ]

In this tutorial you will learn about the JSTL fn endsWith function.

JSTL fn endsWith Example

In this tutorial you will learn about the JSTL fn endsWith function.

To manipulate the string that ends with a specific characters or string in JSP, JSTL provides a function endsWith(). This function helps in to work with the suffix associated with a string.

Syntax :

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

Example :

An example that has given here demonstrates about the JSTL endsWith() function. In this example I have created a JSP page where at first I have designed a form into which took a text field where a value can be entered at run time. Now, since I have to checked that whether an entered text is ends with the specified suffix or not so, I have used the JSTL fn:endsWith() function to check the suffixes. Before doing this on the JSP page I have checked for the conditions whether the value of textbox is null or not, if the value will not be null then the rest part of the page will be executed i.e when you will input the value in the specified textbox and after that you will click on submit button then the respective value/output will be seen to you. The output value will be depend on the condition that I have checked using the endsWith() function i.e if this function will return true then the "Hi" message with the parameter value will be displayed to you otherwise it will displayed the sorry message.

JstlFnEndsWith.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL fn:endsWith Example</title>
</head>
<body>
<form>
<p>Enter a name
<input type="text" name="text1" /></p>
<input type="submit" value="submit"/>
</form>
<%
if (request.getParameter("text1") != null)
{
%> 
<c:set var="str" value="${param.text1}"/>
<c:choose>
<c:when test="${fn:endsWith(str, 'marsh')==true}" >
Hi! ${str}
</c:when>
<c:otherwise>
SORRY! You have entered the wrong name.
</c:otherwise>
</c:choose>
<% 
}
%>
</body>
</html>

How to run this example

Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :

  • jstl.jar
  • standard.jar

After adding of these jar files you may execute your program in the following ways :

  • Select JstlFnEndsWith.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlFnEndsWith.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
  • A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish

NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.

Output :

When the execution process will be completed successfully you will get the output on your eclipse browser as follows :

1.1 When you will enter the value that contains the suffix 'marsh' as follows :

1.2 When you will click on submit button you will get the output as follows :

2.1 When you will enter the value that not contains the suffix 'marsh' as follows :

2.2 On clicking on submit button you will get the output as :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics