The EL expression of the JSP uses

Source: Internet
Author: User
Tags arithmetic operators logical operators


Use of an El expression
El is often used in conjunction with JSTL, but El can be used alone, and the full name is expression Language
In JSP, the contents of some variables are often printed, which can be variables from a range, such as page, request, session, application range.
For example, to print the STR variable, the following three methods are available:
<%out.println (str)%> This will make the JSP page contain Java code;
<%=str%> can only be obtained from the local, if you want to get from the session must be <%=session.getattribute ()%>;
${STR} can be obtained from the request, session, application range.

The syntax format of the EL Expression:
${identifier} (must start with "${" with "}" to end the contents of the "identifier" specific expression)
Notifies the JSP engine to call the Pagecontext.findattribute () method to get an object from each domain object with the identifier as a keyword.
If the object corresponding to the identifier does not exist in the domain object, the result is "" (Note that it is not null).

The variable of the EL expression
The EL Access variable time method is simple, for example ${username}. It means to take out a variable whose name is username in a range.
The name of the property range in El is as shown in the table:
Page
Pagescope

Request
Requestscope

Session
Sessionscope

Application
Applicationscope

Literal constants for El expressions
An EL expression contains variables, literal constants, and operators. Literal constants mainly include strings, numbers, and Booleans, and also null, where
Strings are a string of characters that are caused by any single quotation mark or double quotation mark.
Numeric constants include integer, float, integer representing decimal, hexadecimal, and octal values, floating point type similar to Java and can contain any positive or negative floating-point number.
The Boolean type includes true and false.
${true} ${10} ${10.5f} ${"Hello"} ${null}

Operator of El Expression
Arithmetic operators mainly have the usual "+", "-", "*", "/or div" (* * * Note that this is not divisible * * *), "% or mod"
Relational operators are mainly "= = or eq", "! = or ne", "< or LT", "> or GT", ">= or GE", "<= or le"
Logical operators are mainly "&&", "| |", "!"
Verify the operator "empty" with the conditional operator "?:"
Empty is used as a prefix to retrieve whether a value is null or empty. such as ${empty User.Name} is used to determine whether the value of name in the user object is null
${condition? Truevalue:falsevalue}, if the condition is true, the value of the expression is truevalue, otherwise falsevalue


(i) Implicit objects related to the scope
El implied objects related to the scope include: Pagescope, Requestscope, Sessionscope, and Applicationscope
They can read the values of objects set by the SetAttribute () method using the JSP built-in object PageContext, request, session, and application
-----is getattribute (String name), but no other relevant information is obtained.
For example, to get the value of a username property stored in the session, you can use the following method: Session.getattribute ("username")
In El, use the following method: ${sessionscope.username}
Note: If you do not set the scope for using El Built-in objects, the attribute values are read in order of Pagescope, Requestscope, Sessionscope, and Applicationscope.
(ii) Implicit objects related to input
There are two hidden objects associated with the input, namely Param and paramvalues, which are the more specific hidden objects in El. For example, to get the user's request parameters, you can take advantage of the following methods:
(1) request.getparameter (String name)
(2) request.getparametervalues (String name)
In El, you can use both Param and paramvalues to get the data:
(1) ${param.name}
(2) ${paramvalues.name}

(iii) Other implicit objects
1, the cookie
used to obtain the user's cookie value, such as setting the username attribute value in a cookie, you can use ${cookie.username.value} to obtain the property value.
2, header, and headervalues
Read the header data of the request, using the header or Headervalues built-in object, such as ${header["User-agent"}, Headervalues is used to obtain all header information, which is equivalent to calling the Request.getheaders () method. The
3, Initparam
Initparam is used to read the parameter values set in Web. Xml. For example ${initparam.repeat}, equivalent to: (String) Application.getinitparameter ("repeat"); or
Servletcontext.getinitparameter ("repeat");
4. PageContext
PageContext To obtain additional details about user requirements or pages
${pagecontext.request.querystring} get the requested parameter string
${ PageContext.request.requestURL} Gets the requested URL, not including the parameter string
${pagecontext.request.contextpath} The name of the Web application of the service
${pagecontext.request.method} Get HTTP method (GET, POST)
${pagecontext.request.protocol} Get used Protocol (http/1.1, http/1.0)
${pagecontext.request.remoteuser} Gets the user name
${pagecontext.request.remoteaddr} Gets the user's IP address
${ PageContext.session.new} to determine if the session is the new
${pagecontext.session.id} to get the session ID
${ PageContext.servletContext.serverInfo} Get service information for host side

Disabling of El expressions
(i) The EL expression is enabled by default in JSP 2.0, but if you use other techniques that conflict with the JSP El tag in the JSP page, you can omit the JSP El Identifier by using the Iselignored property of the page directive.
a--<% @page iselignored= "True|false"%>
(1) True: Indicates that the EL expression is ignored for evaluation.
(2) False: Represents the calculated El expression. Default
b--can also modify the Web. XML to determine that the current application does not use JSP EL.
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version= "2.4" >
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>
</web-app>

Functions tag Library for El expressions (function library):
Need to add an instruction
<%@ taglib prefix= "FN" uri= "Http://java.sun.com/jsp/jstl/functions"%>

Common function Descriptions:
Fn:contains (string, substring) returns true if the argument string contains a parameter substring
Fn:containsignorecase (string, substring) returns true if the argument string contains a parameter substring (ignoring case)
Fn:endswith (string, suffix) returns true if the argument string ends with the argument suffix
Fn:escapexml (String) converts XML (and HTML) with special meaning to the corresponding XML character entity code, and returns
Fn:indexof (string, substring) returns the position of the first occurrence of the parameter substring in the parameter string
Fn:join (array, separator) strings a given array of arrays together with the given spacer separator, forming a new string and returning it.
Fn:length (item) returns the number of elements contained in the parameter item. The parameter item type is an array, a collection, or a string. If it is of type string, the return value is the number of characters in string.
Fn:replace (String, before, after) returns a String object. Replaces all occurrences of a parameter before string in a parameter string with a parameter after string, and returns the replaced result
Fn:split (string, separator) returns an array that splits the argument string with the parameter separator, and each part of the split is an element of the array
Fn:startswith (string, prefix) if the argument string starts with the argument prefix, returns true
Fn:substring (string, begin, end) returns the string of the argument string, starting with the argument begin to the argument end position, including the character of the end position
Fn:substringafter (string, substring) returns the argument substring the part of the string that follows the argument string
Fn:substringbefore (string, substring) returns the part of the string that precedes the argument substring in the argument string
Fn:tolowercase (string) turns all the characters in the argument string to lowercase and returns
Fn:touppercase (string) converts all characters of the argument string to uppercase and returns it
Fn:trim (string) strips the trailing and trailing spaces of the parameter string and returns

The EL expression of the JSP uses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.