JSTL true and ‘true’ comparisions and the scope where JSTL looks for variable

This code shows that jstl is smart enough to converts strings to boolean values. Also proves that JSTL looks in all the scopes like ‘request, pageContext, session’. (Refer to my previous post for pageContext). JSTL is also smart enough to compare numbers and strings. Here is the code:

<%–
Document   : JSTL true string comparision and JSTL tag scopes
Created on : Sep 12, 2008, 3:09:31 PM
Author     : gundlapa
–%>

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
http://www.w3.org/TR/html4/loose.dtd”&gt;

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
</head>
<body>
<%
String trueString=”true”;
boolean trueBoolean=true;
request.setAttribute(“trueString”, trueString);
request.setAttribute(“trueBoolean”, java.lang.Boolean.TRUE);
String string1=”1″;
Integer int1=new Integer(1);
request.setAttribute(“string1”, string1);
request.setAttribute(“int1″, int1);
%>
<c:if test=”${trueString}”>
The string is true<BR>
</c:if>
<c:if test=”${trueBoolean}”>
The boolean is true<BR>
</c:if>
<c:if test=”${trueString eq true}”>
The string is true<BR>
</c:if>
<c:if test=”${trueBoolean eq ‘true’}”>
The boolean is true<BR>
</c:if>
<c:if test=”${int1 eq ‘1’}”>
Integer==String<BR>
</c:if>
<c:if test=”${string1 eq 1}”>
String==Integer<BR>
</c:if>
</body>
</html>
The output:

The string is true
The boolean is true
The string is true
The boolean is true
Integer==String
String==Integer

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: