El表达式专门从容器中去内容的
语法:
${容器.key}
e.g.:${requestScope.name}
后台 前台
request requestScope
HTTPSession sessionScope
ServletContext applciationScope
//test.jsp文件
<%@ 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>
<!-- El表达式可以获取4个容器的内容 -->
request:${requestScope.num}<br>
session:${sessionScope.num}
</body>
</html>
作用:
从一个范围里面取值或者从一个对象中取值或是向页面输出值.
1)接收客户端参数.
${param.name1 }
2)指定范围并取值
${pageScope.name2 }
${requestScope.name3 }
${sessionScope.name4 }
${applicationScope.name5 }
3)可以不指定范围再去取值
${name}
这时候会按照pageContext request session application这样一个顺序依次的去找有没有一个叫name的值存在,一旦找到了就输出出来,最终没有找到那么就什么都不输出。
4)取出一个对象中的属性值.
${requestScope.student.id}
${requestScope.student.name}
${requestScope.student.age}
或者
${student.id}
${student.name}
${student.age}
或者
${student["id"]}
${student["name"]}
${student["age"]}
注意:比如 ${student.id}表示是要调用student对象中的getId方法,至于对象中有没有id属性对这个操作没有任何影响.
如果Student类中一个方法是getAddress,返回一个Address类的对象,Address类中有一个方法getCity,这个时候我们就可以这样写去拿到city属性的值.
${student.address.city}
5)输出字符串
${"hello"}
6)输出运算结果或者boolean表达式
${1+1 }
${(1+2)*3-4+5*3 }
${1<3 }
//为空的话返回true
${empty "" }
${empty "hello" }
//取否 不为空的话返回true
${not empty "hello" }
${! empty "hello" }
${param.score >50 }
${param.score >60?"good":"bad" }
7)输出数组、集合中的元素
<%
String[] str = {"hello","world"};
List<String> list = new ArrayList<String>();
list.add("zhangsan");
list.add("lisi");
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("a",100);
map.put("b",200);
map.put("c",300);
request.setAttribute("str",str);
request.setAttribute("list",list);
request.setAttribute("map",map);
%>
${str[0] }<br>
${list[1] }<br>
${map["c"] }<br>
//测试代码:pa.Servlet
package com.briup.web.Servelt;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
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 javax.servlet.http.HttpSession;
import com.briup.web.bean.Address;
import com.briup.web.bean.User;
//http://localhost:8888/jd1812_web/pa?name=tom&age=88
@WebServlet("/pa")
public class paramTest extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request.setAttribute("name", "tom");
// HttpSession session=request.getSession();
// session.setAttribute("name", "liis");
// ServletContext app=getServletContext();
// app.setAttribute("name", "jake");
// User user=new User("jake","2344");
// Address addr=
// new Address("江苏省", "昆山市", "巴城镇");
// user.setAddr(addr);
// request.setAttribute("u", user);
// request.setAttribute("score", 99);
// String[] str={"test","test1","test2"};
// User[] users={
// new User("lisi", "1111"),
// new User("lisi1", "11114"),
// new User("lisi2", "11112"),
// new User("lisi3", "11113")
// };
// List<User> list=new ArrayList<>();
// list.add(new User("lisi", "1111"));
// list.add(new User("lisi1", "1112"));
// list.add(new User("lisi2", "1113"));
// list.add(new User("lisi3", "1114"));
// request.setAttribute("str", list);
Map<String, User> map=
new HashMap<String, User>();
map.put("a", new User("tom", "1111"));
map.put("b", new User("tom", "1111"));
map.put("c d", new User("tom", "1111"));
request.setAttribute("map", map);
request.getRequestDispatcher("/El.jsp")
.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
<!--EL.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<base href="<%=basePath %>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
${requestScope.map}<br>
${requestScope.map["c d"]}<br>
${requestScope.map["c d"].name}<br>
${requestScope.map["c d"].passwd}<br>
${requestScope.map.get("c d")}<br>
<%-- ${map}<br> --%>
<hr>
${requestScope.str.get(2)}
${requestScope.str.get(2).name}
${requestScope.str.get(2).passwd}
<hr>
<%-- ${requestScope.str}<br>
${requestScope.str[0]}<br>
${requestScope.str[0].name}<br>
${requestScope.str[0].passwd}<br> --%>
<%-- ${requestScope.str[1]}<br>
${requestScope.str[2]}<br>
${requestScope.str[3]}<br>
${requestScope.str[4]}<br> --%>
<hr>
${requestScope.score>90?"及格":"不及格"}
<%-- ${param.score>90?"及格":"不及格"} --%>
<hr>
<!-- empty 取反操作 -->
${!empty ""}
${ not empty ""}
<hr>
<!-- 判断一个内容是否为空
empty后面的值也可以是容器中取出的值
-->
${empty " "}<br>
${empty "test"}<br>
${empty requestScope.u1}<br>
<hr>
<!-- el可以输出字符串和表达式 -->
${3<4}<br>
${1+1}<br>
${"test"}<br>
<hr>
<%
//设置值
pageContext.setAttribute("phone", "1212");
%>
<%-- <%=request.getAttribute("name") %>
<%=session.getAttribute("age") %>
<%=application.getAttribute("passwd") %>
<%=pageContext.getAttribute("phone") %> --%>
<hr>
<%-- ${pageScope.phone}
${requestScope.name}
${sessionScope.age}
${applicationScope.passwd} --%>
<%-- 不指定容器
检索key,从pageContext-》request
-》session->application
--%>
<%-- ${phone}
${name}
${age}
${passwd} --%>
<%-- ${requestScope.name}
${sessionScope.name}
${applicationScope.name} --%>
<!-- 直接基于key到容器中找,多个容器
存在相同的key,只会获取第一个 -->
<%-- ${name} --%>
<%-- ${u} --%>
<%-- ${requestScope.u} --%>
<!-- 对象后面的点代表的是property属性 -->
<%-- ${requestScope.u.name}
${requestScope.u.getName()} --%>
<!-- getGo() -->
<%-- ${requestScope.u.go()}
${requestScope.u.name}
${requestScope.u.passwd} --%>
<%-- ${u.go()}
${u.name}
${u.passwd} --%>
<!-- el获取对象,调用对象方法的
时候只能基于下面的写法 u.go()-->
${u.go()}
${u["name"]}
${u["passwd"]}
<hr>
${requestScope.u}<br>
${requestScope.u.name}<br>
${requestScope.u.passwd}<br>
<!-- getAddr() -->
${requestScope.u.addr}<br>
${requestScope.u.addr.province}<br>
${requestScope.u.addr.city}<br>
${requestScope.u.addr.street}<br>
</body>
</html>