Comparison operators in JSP

By: Grenfel Emailed: 1728 times Printed: 2329 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 CheckBox Example
Comparison operators in JSP
JSP Alert Example
JSP Program for display Date
Sending Email using JSP
Uploading an Image to a Database using JSP
Form processing in JSP
Math Object
Server-side plug-Ins
Arithmetic Evaluation Using the Expression Language in JSP
Writing your first JSP page
The Request Object in JSP
The Response Object in JSP
Most Emailed Articles (in JSP)
The Servlet Life Cycle
Drawbacks of the JSP Model 1 Architecture
Working with Status Bar Messages
Embedding a QuickTime movie in a JSP
Embedding MP3 audio file in a JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: ` Permission denied <null>:80
What is JSP?
Cookies using JSP or Java Bean
Click to Activate and Use this control
The JSP Program running first Time.
Server Side Programming
Syntax For JSP Declaratives
Embedding java codes in jsp sciptlets
Tags using in jsp