Comparison operators in JSP

By: Grenfel Emailed: 1646 times Printed: 2114 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

A useful feature of the EL is the ability to perform comparisons, either between numbers or objects. This feature is used primarily for the values of custom tag attributes, but can equally be used to write out the result of a comparison (true or false) to the JSP page. The EL provides the following comparison operators:

• == or eq
• != or ne
• < or lt
• > or gt
• <= or le
• >= or ge

The second version of each operator exists to avoid having to use entity references in JSP XML syntax; however, the behavior of the operators is the same. In the JSP page in Listing below, you can see the comparison operators in use.

<title>EL Conditions</title>
body, td {font-family:verdana;font-size:10pt;}
<h2>EL Conditions</h2>
<table border="1">
<td><b>EL Condition</b></td>
<td>Numeric less than</td>
<td>${'${'}1 &lt; 2}</td>
<td>${1 < 2}</td>
<td>Numeric greater than</td>
<td>${'${'}1 &gt; 2}</td>
<td>${1 > 2}</td>
<td>Numeric less than</td>
<td>${'${'}1 lt 2}</td>
<td>${1 lt 2}</td>
<td>Numeric greater than</td>
<td>${'${'}1 gt 2}</td>
<td>${1 gt 2}</td>
<td>Numeric Greater than or equal</td>
<td>${'${'}1 &gt;= 1}</td>
<td>${1 >= 1}</td>
<td>Numeric Less than or equal</td>
<td>${'${'}1 &lt;= 1}</td>
<td>${1 <= 1}</td>
<td>Numeric less than or equal</td>
<td>${'${'}1 le 1}</td>
<td>${1 le 1}</td>
<td>Numeric greater than or equal</td>
<td>${'${'}1 ge 1}</td>
<td>${1 ge 1}</td>
<td>Numeric equal to</td>
<td>${'${'}1 == 1}</td>
<td>${1 == 1}</td>
<td>Numeric equal to</td>
<td>${'${'}1 eq 1}</td>
<td>${1 eq 1}</td>
<td>Numeric not equal to</td>
<td>${'${'}1 != 2}</td>
<td>${1 != 2}</td>
<td>Numeric not equal to</td>
<td>${'${'}1 ne 2}</td>
<td>${1 ne 2}</td>
<td>Alphabetic less than</td>
<td>${'${'}'abe' &lt; 'ade'}</td>
<td>${'abe' < 'ade'}</td>
<td>Alphabetic greater than</td>
<td>${'${'}'abe' &gt; 'ade'}</td>
<td>${'abe' > 'ade'}</td>
<td>Alphabetic equal to</td>
<td>${'${'}'abe' eq 'abe'}</td>
<td>${'abe' eq 'abe'}</td>
<td>Alphabetic not equal to</td>
<td>${'${'}'abe' ne 'ade'}</td>
<td>${'abe' ne 'ade'}</td>

Again, you’ll deploy the JSP page into a JSP 2.0– or JSP 2.1–compliant web container. Here is the list of steps to create, deploy, and run the JSP page:

  1. Enter the JSP code in Listing 3-4 into a file called conditions.jsp and save it to the expressionLanguage folder.
  2. Start Tomcat, open your web browser, and go to http://localhost:8080/expressionLanguage/conditions.jsp.

Figure shows the web page generated by this JSP page.

Conditional operators can be used to compare operators in a JSP page. Again, parentheses can be used to alter the order of evaluation, and identical precedence operators are evaluated from left to right as follows:

• ()
• - (unary)
• * / div mod %
• + - (binary)
• < > <= >= lt gt le ge
• == != eq ne

JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

Thank you sir.

View Tutorial          By: Balan at 2008-09-02 22:16:56
2. View Comment


View Tutorial          By: maiki at 2015-03-05 15:20:05
3. View Comment


View Tutorial          By: hamza at 2015-06-08 15:09:29

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Grenfel
Verifying a Signed JAR File example
Bitwise and Bit Shift Operators example in Java
JavaBeans Basic Concepts
The Differences Between Simple and Classic Tags in JSP
Bitwise Logical Operators in C
DispatchAction in Struts
union example program in C
Comparison operators in JSP
Basics of Structures in C
register Variables in C
Struts and Tiles - Steps to use Struts and Tiles
The if else statement in C
Character Counting sample program in C
Installing JSF
cin.ignore() in C++

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: ` Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP Example to connect to MS SQL database and retrieve records
What are the different scopes in JSP?
JSP Alert Example
JSP CheckBox Example
JSP Program for display Date
Comparison operators in JSP
Sending Email using JSP
Introduction to JSP expression language
The Advantages of JSP
Arithmetic Evaluation Using the Expression Language in JSP
Automatically Refreshing a JSP
Retrieving a Portion of a String
Writing your first JSP page
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: ` Permission denied <null>:80
Using a DataSource from WebLogic in a JSP
Most Emailed Articles (in JSP)
What is JSP?
Tags using in jsp
Tag libraries
Disabling Scriptlets in JSP using web.xml
Getting HTTP Request Headers in a JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: ` Permission denied <null>:80
What is JSP?
Click to Activate and Use this control
Closing Windows
Frame Object
Math Object
Retrieving a Portion of a String
Introduction to JSP expression language
Cookies using JSP or Java Bean
JSP Program for display Date