Example of Flow Control Tags of JSTL Core Tag Library
Here in this example we will see how to use core tag library of JSTL. URI for JSTL Core library is......
http://java.sun.com/jsp/jstl/core
So before use Core JSTL tags we must include following line of code :-
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
JSTL core library provides following tags for flow
control......
1 :-
if : Tag <c:if
> is used to execute conditional statements.
For Example : <c:if
test="${name=="Mahendra" >
WELCOME........Mr. Mahendra
< /c:if>
Here we are checking
weather name is 'mahendra' or not and execute statement accordingly.
2:-
choose : Tag <c:choose> is used as switch
statement and execute statements accordingly.
For Example : <c:choose>
<c:when test="${name=="Mahendra"}">
Thanx to login as user Mahendra Singh.
</c:when>
<c:when test="${name=="Girish"}">
Thanx to login as user Girish Tewari.
</c:when>
<c:otherwise>
Sorry, unable to login.
<c:otherwise>
</c:choose>
In the example given below defines the use of Flow Control tags of JSTL Core tag
library. We have also used tag <c:out> to show the variable in standard
output device.
jstlExample.jsp
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html> <head> <title>Using Choose,Otherwise and When</title> </head> <body> <c:if test="${pageContext.request.method=='POST'}"> <c:set var="name" value="${param.name}"/> <c:if test="${name=='mahendra'}" > WELCOME........Mr. Mahendra </c:if> <c:if test="${name=='girish'}" > WELCOME........Mr. Girish </c:if><br> <c:choose> <c:when test="${name=='mahendra'}"> Thanx to login as user Mahendra Singh. </c:when> <c:when test="${name=='girish'}"> Thanx to login as user Girish Tewari. </c:when> <c:otherwise> Sorry, unable to login. </c:otherwise> </c:choose> </c:if> <p><b>Hint: Please enter user name 'mahendra' or 'girish' in the following text box.</b></p> <form method="POST">Enter user name : <input type="text" name="name" /> <input type="submit" value="login" /> </form> </body> </html> |
Steps to run this example :
1: Download the zip file of code and unzip this file, you will get a
folder named 'var_support_jstlCore'.
2: Paste this folder in 'Apache Tomcat 6.0.16-->webapps' or generally
in directory 'C:\apache-tomcat-6.0.16\webapps'.
3: Start tomcat server by click on startup.bat file in 'C:\apache-tomcat-6.0.16\bin'.
4: Open browser and type url 'http://localhost:8080/flow_control_coreJstlTag/jstlExample.jsp'
or click on this link.
When program will run in browser this will show the following output.....
when user will enter user name according to the given hint, next response will be.......