The JSP expression language (EL) makes it very easy to access the data stored in JavaBean. JSP El can be used either to create arithmetic expressions or to create logical expressions. You can use integer, floating-point, string, constant True, false, and null within a JSP El expression. Typically when you specify a property value in a JSP tag, you need to use a large line of code:
< Jsp:setproperty name = "box" Property= "Perimeter" value= " /> "
The JSP EL allows you to specify an expression to represent a property value: ${expr}, where expr refers to an expression. The generic operator in the JSP El is. and {}. These two operators allow access to a wide variety of javabean properties through an inline JSP object. For example, the <jsp:setProperty> tag above can be written in the following form using an expression language:
<jsp:setproperty name= "box" property= "Permeter" value= "${box.width*2+box.height*2}" />
When the JSP compiler sees the "${}" format in the attribute, it generates code to evaluate the expression and produces a substitute for the value of the expression.
<jsp:text> Box Perimeter is:${box.width*2+box.height*} </jsp:text>
You can use parentheses to organize an expression in an El expression, to deactivate an evaluation of an El expression, and to set the property value of iselignored with the page directive to true:
<%@ page iselignored= "True|false"%> //default is False, that is, El expression is valid
Set the Iselignored property value to True so that the El expression on this JSP page is invalidated.
Base operator in El
The EL expression supports most of the arithmetic and logical operators provided by Java:
Operator |
Describe |
. |
Access a property of an object or a mapping entry |
[] |
Access an attribute of an object or an array, a list of elements |
() |
Organize a sub-expression to change the order of precedence |
+ |
Add |
- |
Reducing |
* |
By |
/or Div |
Except |
% or mod |
Take surplus |
= = or EQ |
Test for equality |
! = or NE |
Test whether it is unequal |
< or LT |
Test is less than (fewer than) |
> or GT |
Test is greater than (greater than) |
<= or Le |
Test is less than or equal to |
>= or GE |
Test is greater than or equal to |
&& OR and |
Test Logic and |
|| OR OR |
Test logic or |
! Or not |
Test logic is not |
Empty |
Test whether null value |
Functions in the JSP el
The JSP El allows functions to be used in an expression. These functions must be defined in the custom tag library.
${fn:length ("Get my Length")}
The Fn:length function is defined in the Jstl library, where the syntax can get the length of a string, the functions of any tag library need to be installed in the server, and then use the <taglib> tag to reference the libraries in the JSP file.
JSP El Hidden Objects
The JSP El supports the implied objects in the following table:
Implied object |
Describe |
Pagescope |
Page scope |
Requestscope |
Request Scope |
Sessionscope |
Session scope |
Applicationscope |
Application scope |
Param |
Parameters of the Request object, string |
Paramvalues |
Parameters of the Request object, a collection of strings |
Header |
HTTP message header, string |
Headervalues |
HTTP information header, string collection |
Initparam |
Context Initialization Parameters |
Cookies |
Cookie value |
PageContext |
PageContext of the current page |
PageContext Object
The PageContext object is a reference to the PageContext object in the JSP. The request object can be accessed through the PageContext object. For example, access the query string passed in to the Request object:
${pagecontext.request.querystring}
Scope Object
The Pagescope,requestscope,sessionscope,applicationscope variable is used to access variables stored at the various scope levels. If you need to access the test variables in the Session scope field, you can use ${sessionscope.tset}.
Param and Paramvalues objects
The Param and Paramvalues objects are used to access parameter values to replace the Request.getparameter method and Request.getparametervalues method. For example, to access a parameter named order, you can use the EL Expression: ${param.order}, or ${param[order]}. The Param object returns a single string, while the Paramvalues object returns an array of strings.
JSP expression Language (EL)