What is an EL expression? Expression Language is the full name of Expression Language. You can print the value of an expression. As with JSP expression scripts. Evaluates the value of the expression and prints it. The purpose of EL expressions is to make JSP writing easier and to simplify JSP code.
Let’s take a look at a Hello World program for an EL expression and see how it simplifies JSP code. EL expression Hello world program!!
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <! PUBLIC DOCTYPE HTML "- / / / / W3C DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd" > < HTML > < head > < meta http-equiv="Content-Type" content="text/html; Charset =UTF-8"> <title>Insert title here</title> </head> <body> <% // First we need to set a property in the Request domain object Request. setAttribute("hello", "this is the content "); <%=request.getAttribute("hello") = null? "" : the request. The getAttribute (" hello") % > < br / > < br / > < % - the output in the domain to find the value of the output hello - % > EL expression of output: ${hello} < br / > < br / > < / body > < / HTML >Copy the code
From the above procedure, we can easily see. We’re going to print the properties in the field, which is a lot easier.
So EL expressions make JSP page code much more concise. Mainly used to replace expression scripts in JSP.
The primary function of an EL expression is to get data from a domain object and output it
EL expression, get domain object data (***** key)
Syntax for retrieving data using EL expressions: “${identifier}”
If the key of the EL expression does not exist, it will output an empty string. The EL expression searches for attributes in a domain object in the following order: pageContext=====>>>> Request =====>>>> Session disk input application
An EL expression can obtain data from domain objects. 1. Sequence of obtaining domain data by an EL expression When an EL expression statement is executed, it uses an identifier as a key to search for the object corresponding to the key in the page, Request, Session, and Application fields. If found, the corresponding data is returned. An empty string is returned if it cannot be found. (Note, not null, but empty string)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <! PUBLIC DOCTYPE HTML "- / / / / W3C DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd" > < HTML > < head > < meta http-equiv="Content-Type" content="text/html; Charset =UTF-8"> <title>Insert title here</title> </head> <body> <% "Page this is the content "); Request. setAttribute("hello", "request this is the content "); Session. setAttribute("hello", "session this is the content "); Application. setAttribute("hello", "application this is the content "); %> ${hello}<br/> </body> </ HTML >Copy the code
Test steps:
1. Write the page code, directly access the page, output the content of pageContext. 2. Note out the pagecontext. setAttribute code. Refresh the page to print the value of the Request field-wide Hello property. 3. Note out the request.setAttribute code. Refresh the page to print the value of the Session domain-wide Hello property. 4. Annotate the session.setAttribute code, close the browser, and open it again to access the page. Output the application (ServletContext) domain-wide property value 5. Annotate the application.setAttribute code and shut down the server. Then start the server. Open the browser again and visit the page again. There’s no data in the application
2, obtain javaBean general properties, array properties, List properties, to map properties of the data. For example, ${user.username} // get the user object. ${list[subscript]} // Access the element of the ordered set (or array) with the given index ${map.key} // access the attribute value of the map set with the specified key ${map[” key “]} // access the attribute value of the key of the special string
Note: the [] parentheses allow access to elements with sequential collections and arrays. You can also access special key values
Requirement: Create a User object, add string properties, array properties, List collection properties. The map of attributes. Then create an object instance to add to the Request domain object to test fetching
It’s important to remember that when an EL expression gets its data, it gets it through the corresponding get method and when BeanUtils gets its value through the set method
A) First define a JavaBean object ——User class
public class User {
private String username;
private String[] phones;
private Map<String, Object> map;
private List<String> strList;
Copy the code
B) Add some objects to the four domain objects in the JSP page and access the tests using EL expressions.
<%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@page import="java.util.HashMap"%> <%@page import="com.atguigu.servlet.User"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <! PUBLIC DOCTYPE HTML "- / / / / W3C DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd" > < HTML > < head > < meta http-equiv="Content-Type" content="text/html; Charset = utF-8 "> <title>Insert title here</title> </head> <body> <% Add data HashMap<String,Object> map = new HashMap<String,Object>(); map.put("aaa", "AAAValue"); map.put("bbb", "bbbValue"); map.put("ccc", "cccValue"); map.put("ddd", "dddValue"); // create a list collection object list <String> strList = new ArrayList<String>(); strList.add("aaa"); strList.add("bbb"); strList.add("ccc"); // create a User object User User = new User(" username ",new String[]{" first phone "," second phone "," third phone "},map,strList); Request.setattribute ("user", user); % > < % % - the attribute access object - > the username of the user object attribute value -- -- -- - > > > > ${user. The username} < br / > < br / > < % - access to the user object in the array of the second element -- % > The second element of an array of phones in the user object -- -- -- -- -- -- -- -- > > > > ${user. Phones [1]} < br / > < br / > < % - the first element in the access list collection - % > the first element from the set list -- -- -- -- -- -- -- -- > > > > > ${ User. StrList [0]} < br / > < br / > < % - access to the user object map collections of aaa in attribute values -- % > aaa attribute values in the map set in the user object -- -- -- -- -- > > > > > ${user. Map. Aaa} < br / > < br / > </body> </html>Copy the code
The following output is displayed:
Note: Remove unnecessary code validation
EL expression – operation.
Syntax: ${operational expressions}, EL expressions support the following operators:
1) Relational operation
2) Logical operation
3) Arithmetic operations
Empty determines whether a key of an EL expression is empty. It returns true if it is empty and false if it is not.
1. When the value is null. Return true if the value is an empty string; return true if the value is an Object array with a length of zero; return true if the value is a list array with a length of zero; return true if the value is a map array with a length of zero; return true if the value is a list array with a length of zero
Empty tests the code:
<%@page import="java.util.Map"%> <%@page import="java.util.List"%> <%@page import="java.util.HashMap"%> <%@page import="java.util.ArrayList"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <! PUBLIC DOCTYPE HTML "- / / / / W3C DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd" > < HTML > < head > < meta http-equiv="Content-Type" content="text/html; Charset = utF-8 "> <title>Insert title here</title> </head> <body> Return true request.setAttribute("nullObject", null); // If it is an empty string, return true request.setAttribute("emptyStr", ""); // If it is an empty array, return true request.setAttribute("emptyArr", new Object[]{}); // Empty collection, return true List List = new ArrayList(); request.setAttribute("emptyList", list); // Empty map, return true map map = new HashMap(); request.setAttribute("emptyMap", map); ---->>>>${empty nullObject}<br/> empty string, The empty is true -- -- -- -- -- - > > > > ${empty emptyStr} < br / > empty array, the empty is true -- -- -- -- -- - > > > > ${empty emptyArr} < br / > empty list collection, The empty is true -- -- -- - > > > > ${empty emptyList} < br / > empty map collections, the empty is true -- -- -- -- -- > > > > ${empty emptyMap} < br / > < / body > < / HTML >Copy the code
Browser running results:
5) Ternary operation
We can easily use ternary output in EL expressions.
Syntax: ${expression 1? Example: ${12 == 12? "Twelve equals twelve" : "Twelve! = 12 "}Copy the code
We can easily use ternary operators in EL expressions.
${expression 1? Expression 2: expression 3} When expression 1 is true, EL prints the value of expression 2. When expression 1 is false, EL prints the value of expression 3Copy the code
6) “.” The dot and [] parenthesis operators
“.” Operator, which can take the property value of a JavaBean object or the value of a key in a map. [], which can get the element of the ordered collection with the specified index, or the value of a special key.
When we store special keys in a map object. For example. The key character string contains “. , “+”, “-“, “*”, “/”, “%” and so on. Will cause ambiguity in the EL parser. We can use [‘ key ‘] in parentheses and quotation marks.
[] : not only can we get the elements of a given index in an ordered set (array and List), but also the values of keys containing characters of special meaning. For example, key contains “. , +, _, *, /, etc
Example:
<body> <% // Set Map Map = new HashMap(); map.put("a-b-c","a-b-c-Value"); map.put("a.b.c", "a.b.c.Value"); map.put("aaa","aaa-Value"); request.setAttribute("map", map); % > < % - the following brackets means by which we can obtain the corresponding key value - % > ${map [' A.B.C]} < br / > ${map [' a - b - c]} < br / > ${map. Aaa} < br / > < / body >Copy the code
The command output is A.B.C. Value a-b-c-value aaA-value
11 hidden objects in an EL expression.
There are 11 hidden objects in the EL expression, all of which we can use directly!!
The EL expression gets data in the domain object (**** key) pageScope <=== corresponds to ===> pageContext attribute requestScope <=== = corresponds to ===> Request attribute sessionScope <=== = corresponds to properties in the Session field applicationScope <=== ===> corresponds to properties in the ServletContext field
Let’s look at it first. How to get from four domain objects. To retrieve the properties of each domain object, store the data in each of the four domain objects and use pageScope, requestScope, sessionScope, and applicationScope to retrieve the data
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <! PUBLIC DOCTYPE HTML "- / / / / W3C DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd" > < HTML > < head > < meta http-equiv="Content-Type" content="text/html; Charset = utF-8 "> <title>Insert title here</title> </head> <body> <% SetAttribute ("key", "pagecontext-value "); request.setAttribute("key", "request-Value"); session.setAttribute("key", "session-value"); application.setAttribute("key", "application-value"); ${pageScope. Key}<br/> Request key: ${requestScope. Key}<br/> Session key: ${requestScope. Key}<br/> session key: ${requestScope. Key} ${applicationScope.key}<br/> </body> </ HTML >Copy the code
Results of the run:
PageContext accesses the built-in object in the Jsp(not much).
Through the pageContext object. We can get some of the built-in objects in the JSP directly,
Such as:
The request object,
The session object,
Servletconfig object,
ServletContext object,
And get the information we need.
Obtain common functions
Protocol:
Server IP address:
Server port:
Obtaining project path:
Method of obtaining request:
Obtain the client IP address:
Get the session ID number:
PageContext uses sample code
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <! PUBLIC DOCTYPE HTML "- / / / / W3C DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd" > < HTML > < head > < meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" content="no-cache" /> <meta http-equiv="Expires" content="0" /> <meta http-equiv="Content-Type" content="text/html; Charset = utf-8 "> < title > Insert title here < / title > < / head > < body > agreement: ${pageContext. Request. Scheme} < br / > server IP: The ${pageContext. Request. ServerName} < br / > server port: ${pageContext. Request. ServerPort} < br / > the project path: ${pageContext. Request. ContextPath} < br / > get request methods: ${pageContext. Request. Method} < br / > for the client IP address: The ${pageContext. Request. RemoteHost} < br / > for session id number: ${pageContext. Session. Id} < br / > < / body > < / HTML >Copy the code
The most common function of the pageContext object is to get the context path (i.e. the project path name)
Project name (context path) : \ ${pageContext. Request. ContextPath}
But in the actual project. To reduce the amount of code, the Request object is placed inside the pageContext field object. And then use it again, for example
<% // First put the request object into the pageContext field object pagecontext.setAttribute (” req “,request); %> then the EL expression code is changed to the project name (contextPath):\${req.contextPath}
Use of EL expressions for other implicit objects.
Configuration in the web.xml file:
<context-param>
<param-name>username</param-name>
<param-value>root</param-value>
</context-param>
Copy the code
Example code to use:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <! PUBLIC DOCTYPE HTML "- / / / / W3C DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd" > < HTML > < head > < meta http-equiv="Content-Type" content="text/html; Charset = utF-8 "> <title>Insert title here</title> </head> <body> ${paramvalues.hobby [0]}<br/> Accept-language: ${header[" accept-language "]}<br/> Accept-language: ${paramvalues.hobby [0]} ${headerValues["Accept"][0]}<br/> cookie key = ${cookie.jsessionID.name} : Value = ${cookie.jsessionID. value}<br/> Context parameters: ${initparam. username}<br/> </body> </ HTML >Copy the code
The result of the visit: