Comparison operators in JSP

By: Grenfel Emailed: 1678 times Printed: 2158 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.

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

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

Comments(3)


1. View Comment

Thank you sir.

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

Gracias.

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

1

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: `java.net.BindException: 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
Automatically Refreshing a JSP
JSP Program for display Date
JSP CheckBox Example
What are the different scopes in JSP?
JSP Alert Example
Comparison operators in JSP
Uploading an Image to a Database using JSP
Sending Email using JSP
The BodyTag Interface in JSP
Arithmetic Evaluation Using the Expression Language in JSP
Writing your first JSP page
The Advantages of JSP
Uploading a file to a server using JSP
Sessions in JSP
Most Emailed Articles (in JSP)
Textarea Object
JSP Example to connect to MS SQL database and retrieve records
Click to Activate and Use this control
Document Object
What is JSP?
Debugging Servlets
The Servlet Life Cycle
Frame Object
NotifyUtil::java.net.ConnectException: Connection refused: connect
The page Directive in JSP
A JSP page that gets properties from a bean
Uploading an Image to a Database using JSP
The JSP Program running first Time.
Embedding java codes in jsp sciptlets
Sessions in JSP