Use of EL expressions in Jsp and JspEL expressions

Source: Internet
Author: User
Tags arithmetic operators

Use of EL expressions in Jsp and JspEL expressions
Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka
1. What is EL?EL is a JSP Expression Language, and its full name is ExpressionLanguage. The purpose of EL is to simplify the way variables are accessed in JSP, and the coupling between simple static HTML and Java code.
Here is an example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

Here we use EL to write:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

The code is much less and clearer than the original one.
2. jsp el expressions are used in the following scenarios:Standard and custom static text labels are installed using the serviner of Servlet2.4/JSP2.0. 3. Basic syntax format

$ {EL Expression}

Example: $ {"Helloworld"} // output String constant $ {str} // output string variable str value ${3 + 2} // output result of 3 + 2 $ {user. name} // output the name attribute of the user object $ {user ["name"]} // same as $ {sessionScope ["user"]. name} // same as $ {user. name} The getName () method of the Access Object user to obtain the value of the name Member. $ {List [1]} accesses the second item of the list object. $ {Map ["key"]} accesses the value of the specified key of map.
Let's look at an example.

EL's syntax is very simple. Its biggest feature is its ease of use.
Example:

${sessionScope.user.sex}
All EL types start with $ {and end.
The preceding EL example indicates that the user's gender is obtained from the Session. The preceding JSP code is written as follows:
<% User user = (User)session.getAttribute("user"); String sex = user.getSex( ); %>
In comparison, we can find that EL syntax is more convenient and concise than traditional JSP code.

EL provides two operators, "." and "[]", to access data. [] can access the elements and Bean attributes of a set or array. The following two represent the same meaning, but you must ensure that the property of the object to be obtained has the corresponding setXxx () and getXxx () methods.

Example:

${sessionScope.user.sex}
Equal

${sessionScope.user["sex"]}

. And [] can also be used together, as follows:

${sessionScope.shoppingCart[0].price}
The returned result is the price of the first item in shoppingCart.

4. Similarities and Differences between "." and "[]"All objects can be accessed with properties. Difference: When the attribute name contains complex symbols such as spaces and periods. An exception will occur when "." is used to access an object with properties. 5. OperatorsArithmetic Operators (+,-, *,/, %) logical operators (&, | ,! Or and, or, not) XML operator lt <le <= gt> ge> = comparison operator (>,>=, <,<=, == ,! =)-The null Operator (empty) of the data type can be automatically converted. // if the value is null, true is returned.

EL's Arithmetic Operators are roughly the same as those in Java and have the same priority.

Note: The '+' operator does not connect to a string. It is only used for addition operations.

EL Relational operators include the following six operators:

Relational operators
= Or eq | equal to | ${5 = 5} or ${5 eq 5} | true
! = Or ne | not equal to | $ {5! = 5} or ${5 ne 5} | false
<Or lt | less than | ${3 <5} or ${3 lt 5} | true
> Or gt | greater than | $ {3> 5} or ${3 gt 5} | false
<= Or le | less than or equal to | ${3 <= 5} or ${3 le 5} | true
>=Or ge | greater than or equal to | ${3 >=5} or ${3 ge 5} | false


6. Set accessArray access

$ {} // Request. getAttribute ("name ");

List Access Map access
7. Implicit object

JSP Expression Language defines a set of implicit objects, many of which are available in JSP scriplet and expressions:





In addition, it provides several implicit objects that allow simple access to the following objects:


Terms Definition

Param

Map the request parameter name to a single String parameter value (obtained by calling ServletRequest. getParameter (String name ). The getParameter (String) method returns a parameter with a specific name. Expression $ (param. name) is equivalent to request. getParameter (name ).

ParamValues

Map the request parameter name to a numeric array (obtained by calling ServletRequest. getParameter (String name ). It is very similar to a param implicit object, but it retrieves a string array instead of a single value. Expression $ {paramvalues. name) is equivalent to request. getParamterValues (name ).

Header

Map the request header name to a single String header value (obtained by calling ServletRequest. getHeader (String name ). The expression $ {header. name} is equivalent to request. getHeader (name ).

HeaderValues

Map the request header name to a numeric array (obtained by calling ServletRequest. getHeaders (String ). It is very similar to the header implicit object. The expression $ {headerValues. name} is equivalent to request. getHeaderValues (name ).

Cookie Map the cookie name to a single cookie object. Client requests sent to the server can obtain one or more cookies. Expression $ {cookie. name. value} returns the first cookie value with a specific name. If the request contains multiple cookies with the same name, use the $ {headerValues. name} expression.
InitParam Map the context initialization parameter name to a single value (obtained by calling ServletContext. getInitparameter (String name ).

In addition to the preceding two types of implicit objects, some objects allow access to variables in various scopes, such as Web context, session, request, and page:


Terms Definition

Pagination

Map the variable names in the page range to their values. For example, an EL expression can use $ {pagination. objectName} to access a page range object in a JSP, and use $ {pagination. objectName. attributeName} to access the object attributes.

RequestScope

Map the variable name in the request range to its value. This object allows access to the properties of the request object. For example, the EL expression can use $ {requestScope. objectName} to access an object in the JSP request range, and use $ {requestScope. objectName. attributeName} to access the attributes of the object.

SessionScope

Map the name of the variable in the session range to its value. This object allows access to the properties of the session object. For example:


$ SessionScope. name}

ApplicationScope

Map the variable names in the application range to their values. This implicit object allows access to objects within the application scope


Note:

Cookie object

The so-called cookie is a small text file. It records the SessionTracking content in the text file in key and value mode. This text file is usually stored in the temporary storage area of the browser. JSTL does not provide the cookie setting action, because this action is usually required by backend developers, rather than being handed over to front-end developers. If we set a value named userCountry in the cookie, we can use $ {cookie. userCountry} to get it.

Header and headerValues(Request header object)
The header stores the data used by the browser and server for communication. When the user requests the webpage of the server, a header file that records the required information is sent, for example: the user's browser version, the region set by the user's computer, and other related data. To obtain the version of the User's browser, that is, $ {header ["User-Agent"]}. In addition, it is rare that the same header name may have different values. In this case, you must use headerValues to obtain these values.
Note: Because the User-Agent contains the special character "-", you must use "[]" instead of $ {header. User-Agent }.
InitParam
Like other attributes, we can set the environment parameters (Context) of the web application. To obtain these parameters, we can use the initParam implicit object to obtain them. For example: when we are in the web. the settings in xml are as follows:

<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<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/web-app_2_4.xsd"
Version = "2.4">
<Context-param>
<Param-name> userid </param-name>
<Param-value> mike </param-value>
</Context-param>
</Web-app>
Then we can directly use $ {initParam. userid} to obtain the parameter named userid with the value of mike. The following is the previous practice: String userid = (String) application. getInitParameter ("userid ");

PageContext object

We can use $ {pageContext} to obtain other details about user requirements or pages. The following lists some common parts.
Expression description
$ {PageContext. request} | get the request object
$ {PageContext. session} | obtains the session object.
$ {PageContext. request. queryString} | obtains the request parameter string.
$ {PageContext. request. requestURL} | obtains the request URL, excluding the request parameter string.
$ {PageContext. request. contextPath} | Name of the web application of the service
$ {PageContext. request. method} | get http (GET, POST)
$ {PageContext. request. protocol} | used protocol (HTTP/1.1, HTTP/1.0)
$ {PageContext. request. remoteUser} | get the user name
$ {PageContext. request. remoteAddr} | obtains the user's IP address.
$ {PageContext. session. new} | judge whether the session is new. The so-called new session indicates that the session was generated by the server and has not been used by the client.
$ {PageContext. session. id} | obtains the session ID.
$ {PageContext. servletContext. serverInfo} | obtains the service information of the host.

8. Special emphasis 1. Note that when the expression references these objects based on their names, the corresponding objects are returned instead of the corresponding attributes. For example, even if the existing pageContext attribute contains some other values, $ {pageContext} returns the PageContext object.

2. Note that <% @ pageisELIgnored = "true" %> indicates whether the EL language is disabled. TRUE indicates that the EL language is disabled. FALSE indicates that the EL language is disabled by default in. JSP2.0.

Copyright Disclaimer: This article is the original article by the blogger Lin bingwen Evankaka, which cannot be reproduced without the permission of the blogger.

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.