Comparison operators in JSP
By: Grenfel in JSP Tutorials on 2007-09-23
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 < 2}</td> <td>${1 < 2}</td> </tr> <tr> <td>Numeric greater than</td> <td>${'${'}1 > 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 >= 1}</td> <td>${1 >= 1}</td> </tr> <tr> <td>Numeric Less than or equal</td> <td>${'${'}1 <= 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' < 'ade'}</td> <td>${'abe' < 'ade'}</td> </tr> <tr> <td>Alphabetic greater than</td> <td>${'${'}'abe' > '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:
- Enter the JSP code above into a file called conditions.jsp and save it to the expressionLanguage folder.
- 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
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
Comments