EL表达式
EL是JSP 2.0增加的技术规范,其全称是表达式语言(Expression Language)。EL语言是一种在页面中访问数据的语言,简洁,易于维护。
1.EL语法
在JSP中访问模型对象是通过EL表达式的语法来表达。所有EL表达式的格式都是以“${}”表示。
例如,${ userinfo}代表获取变量userinfo的值。
2.EL表达式访问,PageContext,Request,Session,Application中的对象
页面中有四个范围(scope),分别是pageScope页面范围,requestScope请求范围,sessionScope会话范围,applicationScope服务器范围
示例:先创建一个Servlet,在上述四个范围中存入数据,转发到index.jsp页面
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "DataServlet",urlPatterns = "/data")
public class DataServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//存数据
request.setAttribute ("a","个人练习生");
request.setAttribute ("b","蔡徐坤");
request.setAttribute ("c","鸡你太美");
request.getSession ().setAttribute ("a","个人练习生");
request.getSession ().setAttribute ("b","蔡徐坤");
request.getSession ().setAttribute ("c","鸡你太美");
request.getServletContext ().setAttribute ("a","个人练习生");
request.getServletContext ().setAttribute ("b","蔡徐坤");
request.getServletContext ().setAttribute ("c","鸡你太美");
//转发
request.getRequestDispatcher ("/index.jsp").forward (request,response);
}
}
在index.jsp页面中使用范围取值的EL表达式 :
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h3>获取request中的常量</h3>
<h4>
a:${requestScope.a}<br/>
b:${requestScope.b}<br/>
c:${requestScope.c}<br/>
</h4>
<h3>获取Session中的常量</h3>
<h4>
a:${sessionScope.a}<br/>
b:${sessionScope.b}<br/>
c:${sessionScope.c}<br/>
</h4>
<h3>获取servletContext中的常量</h3>
<h4>
a:${applicationScope.a}<br/>
b:${applicationScope.b}<br/>
c:${applicationScope.c}<br/>
</h4>
<h3>EL表达式访问范围的默认顺序 page=>session=>application</h3>
<h4>
a:${a}<br/>
b:${b}<br/>
c:${c}<br/>
</h4>
</body>
</html>
输出结果:
3.EL表达式访问对象
首先如果存在某Scope范围的数据是对象,我们要访问对象中的属性时,使用.引用法,如${student.name}
示例:
创建Student对象,在Servlet中存入request
创建Student对象及其内的Adress对象
//Student
package edu.xalead.entity;
public class Student {
public Student(int id, String name, int age,Address adr) {
this.id = id;
this.name = name;
this.age = age;
this.address = adr;
}
public Student() {
}
private int id;
private String name;
private int age;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
private Address address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//Address
package edu.xalead.entity;
public class Address {
private String homeAdr;
public Address(String homeAdr, String busiAdr) {
this.homeAdr = homeAdr;
this.busiAdr = busiAdr;
}
private String busiAdr;
public String getHomeAdr() {
return homeAdr;
}
public void setHomeAdr(String homeAdr) {
this.homeAdr = homeAdr;
}
public String getBusiAdr() {
return busiAdr;
}
public void setBusiAdr(String busiAdr) {
this.busiAdr = busiAdr;
}
}
创建ObjectDataServlet :
import edu.xalead.star.Address;
import edu.xalead.star.Student;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "ObjectDataServlet",urlPatterns = "/obj")
public class ObjectDataServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student stu = new Student (10088,"蔡徐坤",23,new Address ("韩国","湖南卫视"));
//把对象传入request
request.setAttribute ("stu",stu);
//转发到index.jsp
request.getRequestDispatcher ("/index1.jsp").forward (request,response);
}
}
创建转发到的页面index1.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
<meta charset="utf-8"/>
</head>
<body>
<h3>EL表达式获取对象中的数据</h3>
<h4>
Student对象的<br/>
id属性 : ${s.id} <br/>
name属性 : ${s.name} <br/>
age属性 : ${s.age} <br/>
Address属性:<br/>
homeAdr: ${s.address.homeAdr}<br/>
busiAdr: ${s.address.busiAdr}<br/>
</h4>
</body>
</html>
输出结果
4.EL表达式访问集合
示例:
创建保存List,Set,Map集合的ListDataServlet
import edu.xalead.entity.Address;
import edu.xalead.entity.Student;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
@WebServlet(name = "ListDataServlet",urlPatterns = "/list")
public class ListDataServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建List集合
List<Student> students = new ArrayList<>();
students.add(new Student(2222,"张三",20,new Address("西安","北京")));
students.add(new Student(2232,"李四",22,new Address("西安","北京")));
students.add(new Student(3322,"王麻子",21,new Address("西安","北京")));
//存入request
request.setAttribute("list",students);
// 创建Set集合
Set<Student> ss = new HashSet<>();
ss.add(new Student(2222,"张三",20,new Address("西安","北京")));
ss.add(new Student(2232,"李四",22,new Address("西安","北京")));
ss.add(new Student(3322,"王麻子",21,new Address("西安","北京")));
// 存入request
request.setAttribute("ss",ss.toArray());
//访问map集合的数据
Map<String,Student> map = new HashMap<>();
map.put("2222",new Student(2222,"张三",20,new Address("西安","北京")));
map.put("2232",new Student(2232,"李四",22,new Address("西安","北京")));
map.put("3322",new Student(3322,"王麻子",21,new Address("西安","北京")));
//存入request
request.setAttribute("map",map);
// 转发给index2.jsp
request.getRequestDispatcher("/index2.jsp").forward(request,response);
}
}
注意,set集合EL表达式没法直接访问其中的元素,所以我们先ss.toArray()转换成了数组
创建index2.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>访问List集合的数据</h3>
<h4>
访问第一个元素: ${list[0].id} : ${list[0].name} : ${list[0].age} <br/>
访问第三个元素: ${list[2].id} : ${list[2].name} : ${list[2].age}<br/>
</h4>
<h3>访问Set集合的数据</h3>
<h4>
访问第一个元素: ${ss[0].id} : ${ss[0].name} : ${ss[0].age} <br/>
访问第三个元素: ${ss[2].id} : ${ss[2].name} : ${ss[2].age}<br/>
</h4>
<h3>访问Map集合的数据</h3>
<h4>
访问键为2232元素: ${map["2232"].id} : ${map["2232"].name} : ${map["2232"].age} <br/>
访问第三个元素: ${map["3322"].id} : ${map["3322"].name} : ${map["3322"].age}<br/>
</h4>
</body>
</html>
输出结果:
5.EL访问参数(查询字符串,表单的属性),头,cookie,InitParam
EL表达式中通过params访问参数,通过header访问头属性,通过cookie访问cookie
示例:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>EL表达式访问查询字符串参数</h3>
<h4>
name:${param["name"]}<br/>
sex:${param.sex}<br/>
</h4>
<h3>EL表达式访问表单</h3>
<h4>
name:${param["account"]}<br/>
sex:${param.password}<br/>
</h4>
<h3>EL表达式访问头</h3>
<h4>
Host:${header["Host"]}<br/>
cookie:${header.cookie}<br/>
</h4>
<h3>EL表达式访问Cookie</h3>
<h4>
JSeesionId:${cookie["JSESSIONID"].name}:${cookie["JSESSIONID"].value}<br/>
Password:${cookie.password.name}:${cookie["password"].value}<br/>
</h4>
</body>
</html>
访问contextParam
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>driverClass</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://192.168.93.88:3306/mydb</param-value>
</context-param>
</web-app>
在页面中添加
<h3>EL表达式访问Context-Param</h3>
<h4>
driverClass:${initParam["driverClass"]}<br/>
url:${initParam.url}<br/>
</h4>