Programming Tutorials

(追記) (追記ここまで)

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 &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 above 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



(追記) (追記ここまで)


Add Comment

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!
(追記) (追記ここまで)
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /