The JSP 2.0 expression language defines a number of arithmetic, relational, logical, and missing-value-testing operators. Although use of the operators often results in code that is a bit shorter than the equivalent Java code, you should keep in mind the main purpose of the expression language: to provide concise access to existing objects, usually in the context of the MVC architecture. So, we see little benefit in replacing long and complex scripting elements with only slightly shorter expression language elements. Both approaches are wrong; complex business- or data-access -logic does not belong in JSP pages at all. As much as possible, restrict the use of the expression language to presentation logic; keep the business logic in normal Java classes that are invoked by servlets. Core Approach
Arithmetic OperatorsThese operators are typically used to perform simple manipulation of values already stored in beans. Resist using them for complex algorithms; put such code in regular Java code instead.
Relational OperatorsThese operators are most frequently used with either the JSP expression language conditional operator (Section 16.10) or with custom tags whose attributes expect boolean values (e.g., looping tags like those in the JSP Standard Tag LibraryJSTLas discussed in Volume 2 of this book).
Logical OperatorsThese operators are used to combine results from the relational operators.
The empty Operator
An ExampleListing 16.11 illustrates several of the standard operators; Figure 16-6 shows the result. Listing 16.11 operators.jsp<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD><TITLE>EL Operators</TITLE> <LINK REL=STYLESHEET HREF="/el/JSP-Styles.css" TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> EL Operators </TABLE> <P> <TABLE BORDER=1 ALIGN="CENTER"> <TR><TH CLASS="COLORED" COLSPAN=2>Arithmetic Operators <TH CLASS="COLORED" COLSPAN=2>Relational Operators <TR><TH>Expression<TH>Result<TH>Expression<TH>Result <TR ALIGN="CENTER"> <TD>${3+2-1}<TD>${3+2-1} <%-- Addition/Subtraction --%> <TD>${1<2}<TD>${1<2} <%-- Numerical comparison --%> <TR ALIGN="CENTER"> <TD>${"1"+2}<TD>${"1"+2} <%-- String conversion --%> <TD>${"a"<"b"}<TD>${"a"<"b"} <%-- Lexical comparison --%> <TR ALIGN="CENTER"> <TD>${1 + 2*3 + 3/4}<TD>${1 + 2*3 + 3/4} <%-- Mult/Div --%> <TD>${2/3 >= 3/2}<TD>${2/3 >= 3/2} <%-- >= --%> <TR ALIGN="CENTER"> <TD>${3%2}<TD>${3%2} <%-- Modulo --%> <TD>${3/4 == 0.75}<TD>${3/4 == 0.75} <%-- Numeric = --%> <TR ALIGN="CENTER"> <%-- div and mod are alternatives to / and % --%> <TD>${(8 div 2) mod 3}<TD>${(8 div 2) mod 3} <%-- Compares with "equals" but returns false for null --%> <TD>${null == "test"}<TD>${null == "test"} <TR><TH CLASS="COLORED" COLSPAN=2>Logical Operators <TH CLASS="COLORED" COLSPAN=2><CODE>empty</CODE> Operator <TR><TH>Expression<TH>Result<TH>Expression<TH>Result <TR ALIGN="CENTER"> <TD>${(1<2) && (4<3)}<TD>${(1<2) && (4<3)} <%--AND--%> <TD>${empty ""}<TD>${empty ""} <%-- Empty string --%> <TR ALIGN="CENTER"> <TD>${(1<2) (4<3)}<TD>${(1<2) (4<3)} <%--OR--%> <TD>${empty null}<TD>${empty null} <%-- null --%> <TR ALIGN="CENTER"> <TD>${!(1<2)}<TD>${!(1<2)} <%-- NOT -%> <%-- Handles null or empty string in request param --%> <TD>${empty param.blah}<TD>${empty param.blah} </TABLE> </BODY></HTML> Figure 16-6. The expression language defines a small set of operators. Use them with great restraint; invoke business logic from servlets, not from JSP pages.
|