1. Introduction of EL expression
-
Expression language- Expression language
-
An easy way to get data in a JSP page (only get data, not modify it)
-
Introduced in jsp2.0
Syntax format
${expression}
The above format can be used to obtain the value of the specified expression in any static part of the JSP page
2, EL get data (get attributes from four domains)
1. Four fields in EL expressions
- PageContext: Variable is valid only on the defined page, scoped to the current page
- Request: Variables are valid within a request, that is, variables can be moved from one page to another, scoped to a request
- Session: Variables are valid within a session, can span multiple pages, and are scoped to a session
- Application: Since it is on the server side, the scope is for the entire project
2. Default lookup
If no built-in object of EL is used, the order of finding data is to find the property values of the specified name from the four fields in ascending order
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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>
<%
pageContext.setAttribute("name"."linjie");
request.setAttribute("name"."lucy");
session.setAttribute("name"."king");
application.setAttribute("name"."bilibili");
%>
name=${name }
</body>
</html>
Copy the code
Output: name = linjie
3. Search efficiency
Use EL built-in objects to retrieve data from specified domains, improving lookup efficiency
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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>
<%
pageContext.setAttribute("name"."linjie");
request.setAttribute("name"."lucy");
session.setAttribute("name"."king");
application.setAttribute("name"."bilibili");
%>
name=${applicationScope.name }
</body>
</html>
Copy the code
Output: name = bilibili
4, summary
The JSP can only fetch specified data from four fields
No search scope is specified. By default, the value of the specified attribute is found from smallest to largest
PageContext < Request <session< Application
Built-in objects in EL
EL has 11 built-in objects, which are related to the domain properties of 4 and 11 built-in objects of other 4 EL, except for pageContext, the other 10 built-in objects are java.util.Map type
The name of the | type | describe |
---|---|---|
pageScope | java.util.Map | Get the pageContext field attribute, equivalent to pagecontext.getAttribute (” XXX “) |
requestScope | java.util.Map | Get the request domain attribute, equivalent to request.getAttribute(” XXX “) |
sessionScope | java.util.Map | Get session domain attributes, equivalent to session.getAttribute(” XXX “) |
applicationScope | java.util.Map | Get the application domain attribute, equivalent to application.getAttribute(” XXX “) |
param | java.util.Map | Request.getparameter (“key”); request.getParameter(“key”); request.getParameter(“key”) |
paramValues | java.util.Map | Corresponding parameters, it is a Map, which is the key parameter, the value is more than one parameter values, suitable for multivalued parameters, equivalent to the request. GetParameterValues (” key “) |
initParam | java.util.Map | Get the parameter ${initparam.xxx} in web.xml, XXX is the value in the tag, and then get the value in |
pageContext | javax.servlet.ServletContext | You can get request, Response, session, servletContext, servletConfig and other nine built-in objects in JSP |
cookie | java.util.Map | Map<String, cookie >, where key is the name of the cookie and value is the cookie object. For example, ${cookie.jsessionID. value} is the sessionId |
header | java.util.Map | GetHeader (“key”) = request.getheader (“key”); |
HeaderValues: | java.util.Map | GetHeaders (“key”) is a Map where key is the header name and value is the multi-header value. This applies to multi-valued headers, just like request.getHeaders(“key”) |
1. Four related domain attributes
PageScope, requestScope, sessionScope, applicationScope
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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>
<%
pageContext.setAttribute("name"."linjie");
request.setAttribute("name"."lucy");
session.setAttribute("name"."king");
application.setAttribute("name"."bilibili");
%>
name=${applicationScope.name }<br>
name=${pageScope.name }<br>
name=${sessionScope.name }<br>
name=${requestScope.name }<br>
</body>
</html>
Copy the code
Output:
name=bilibili
name=linjie
name=king
name=lucy
2. Other important built-in objects
- pageContext
package linjie.com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Regster extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}Copy the code
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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 > < % - ${pageContext. Request. ContextPath} represents the root web application -- % > < form action ="${pageContext.request.contextPath }/regster" method="POST">
xxx<input type="text" name="name"/><br>
yyy<input type="text" name="age"/><br>
<input type="submit" value="Click">
</form>
</body>
</html>
Copy the code
Output: The root directory of the Web project
- param
The underlying call actually calls Request.getParameter ()
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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 > < % - ${pageContext. Request. ContextPath} represents the root web application -- % > < form action ="${pageContext.request.contextPath }/show.jsp" method="POST">
xxx<input type="text" name="name"/><br>
yyy<input type="text" name="age"/><br>
<input type="submit" value="Click">
</form>
</body>
</html>
Copy the code
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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>
name=${param.name }<br>
age=${param.age }<br>
</body>
</html>
Copy the code
Enter XXX: zhangsan YYy: 20 in the browser on the client
Result: Name =zhangsan age=20
- paramValues
Get request in all of the specified argument value, its underlying actual call request. GetParameterValues ()
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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 > < % - ${pageContext. Request. ContextPath} represents the root web application -- % > < form action ="${pageContext.request.contextPath }/show.jsp" method="POST">
xxx<input type="text" name="name"/><br>
yyy<input type="text" name="age"/><br"checkbox" name="hobby" value="sleep"> sleep <input type="checkbox" name="hobby" value="play"Play > < input type ="checkbox" name="hobby" value="eat"Eat > < input type ="submit" value="Click">
</form>
</body>
</html>
Copy the code
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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>
name=${param.name }<br>
age=${param.age }<br>
hobby[0]=${paramValues.hobby[0] }<br>
hobby[1]=${paramValues.hobby[1] }<br>
</body>
</html>
Copy the code
- initParam
Initialization parameters, its underlying calls the ServletContext. GetInitParameter ()
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>07eltttt</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<! -- Initialization parameters -->
<context-param>
<param-name>name</param-name>
<param-value>Zhang SAN</param-value>
</context-param>
<servlet>
<display-name>Regster</display-name>
<servlet-name>Regster</servlet-name>
<servlet-class>linjie.com.Regster</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Regster</servlet-name>
<url-pattern>/regster</url-pattern>
</servlet-mapping>
</web-app>
Copy the code
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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>
name=${initParam.name }
</body>
</html>
Copy the code
Output: name= Zhang SAN
EL accesses the Bean object
1. What is a Bean object
JavaBean objects are common Java classes, but they must meet the following requirements for editing tools to recognize them:
- Having a public default constructor (such as a no-parameter constructor)
- Properties are accessed using the public get and set methods, that is, set to private and the size of the get and set methods must correspond to the property name. For example, the property name, the get method would be public String getName(){}, capital N
- Serialization is required. This is necessary for the framework and tools to reflect state across platforms
2. How to access
EL can get the specified value as ${key. property}, which actually calls the get method of the corresponding property of the object
package com.zjj.stu;
/* *Bean */
public class Demo {
private String name;
private int age;
public Demo(String name,int age){
this.name=name;
this.age=age;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge(a) {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString(a) {
return super.toString(); }}Copy the code
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="linjie.com.Demo"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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>
<%
Demo test=new Demo("zhangsan".12);
request.setAttribute("ELttt", test); %> name=${requestScope.ELttt.name }<br> age=${requestScope.ELttt.age }<br> <! -- If access isnullThe object's properties, EL won't throw out null pointer exception, just don't show - > names = ${requestScope. ELtttxx. Name} < br > < / body > < / HTML >Copy the code
Output:
name=zhangsan
age=test
Names = (actually a null character is printed here)
EL accesses the data in the array
1. Access data from a simple array
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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>
<%
String[] names={"xlj"."lucy"."king"};
pageContext.setAttribute("names", names);
%>
name[1]=${names[1] }<br> <! -- If the index of an array element exceeds the upper index of the array, the EL does not throw an out-of-bounds exception, but does not display --> names[5]=${names[5] }<br>
</body>
</html>
Copy the code
2. Access the data in the object array
package com.zjj.stu;
/* *Bean */
public class Stu {
private String sname;
private String address;
public Stu(a) {
super(a); }public Stu(String sname, String address) {
super(a);this.sname = sname;
this.address = address;
}
public String getSname(a) {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getAddress(a) {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString(a) {
return super.toString(); }}Copy the code
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="linjie.com.*"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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>
<%
Stu[] stus=new Stu[3];
stus[0] =new Stu("xlj"."A");
stus[1] =new Stu("lucy"."B");
stus[2] =new Stu("kingA"."C");
pageContext.setAttribute("stus",stus);
%>
stus[1].Sname=${stus[1].sname }
</body>
</html>
Copy the code
Output: stus [1]. The Sname = Lucy
EL retrieves data from list
The < %@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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>
<%
List<String> names=new ArrayList<String>();
names.add("xlj");
names.add("lucy");
pageContext.setAttribute("names", names); % > <! --> names[1]=${names[1] }<br>
</body>
</html>
Copy the code
Output: names [1] = Lucy
Note: EL can access a List through a subscript index, but not a Set because there is no index concept in a Set
7. EL accesses Map
Get it in the ${object.key} format
The < %@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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>
<%
Map<String,Object> map=new HashMap<String,Object>();
map.put("age".20);
map.put("name"."zhangsan");
pageContext.setAttribute("map", map);
%>
name=${map.name }<br>
age=${map.age }<br>
</body>
</html>
Copy the code
Output:
name=zhangsan
age=20
Operators in EL
1. Common operators
- Arithmetic operators: +, -, *, /, %(++, — not supported)
- Relational operators: ==,! =, >, >=, <, <=
- Logical operators:! , &&, | |, the not, and, or
- Conditional operators:? :
- Value operators: {}, dot (.)
<%@ page contentType="text/html; charset=UTF-8" language="java"% > < HTML > < head > < title > title < / title > < / head > < body > < % - the arithmetic operators - % >1+2 = ${1+2}<br> <%-- relational operator --%>1= =2 = ${1= =2}<br>
1! =2 = ${1! =2}<br> <%-- logical operator --%>true&&false = ${true && false}<br>
true||false = ${true || false}<br> <%-- the conditional operator expression istrue, select the former --%> ${2+3>4 ? 5 : 4}<br>
${2+3<4 ? 4 : 5}
</body>
</html>
Copy the code
2. Empty operator
The return type is Boolean, true if null, false if otherwise
The < %@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / 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>Title</title>
</head>
<body>
<%
String name1=null;
String name2="";
List<String> name3=new ArrayList<String>();
pageContext.setAttribute("name1", name1);
pageContext.setAttribute("name2", name2);
pageContext.setAttribute("name3", name3); %> empty For undefined variables, the result of the operation istrueEmpty namex=${empty namex}<br> empty fornullThe result of the operation istrueEmpty name1=${empty name1}<br> empty For a String reference that is an empty String, the result istrue: empty name2=${empty name2}<br> empty For an array or set without elements, the result istrueEmpty name3=${empty name3}<br> </body> </ HTML >Copy the code
Ix. Summary of EL
- EL expressions cannot appear in Java code blocks, expression blocks, and other JSP dynamic code parts
- EL can only fetch data from four domains (pageContext, Request, Session, Application).
- EL does not throw a null pointer exception, only does not display it
- EL does not throw array out-of-bounds exceptions, only does not display them
- EL does not have the ability to manipulate strings (you can use JSTL’s EL or custom EL functions)