1.EL 表達式概述
EL(Express Lanuage)表達式可以嵌入在jsp頁面內部,減少jsp腳本的編寫,EL 出現的目的是要替代jsp頁面中腳本的編寫。
2.EL從域中取出數據(EL最重要的作用)
jsp腳本:<%=request.getAttribute(name)%>
EL表達式替代上面的腳本:${requestScope.name}
EL最主要的作用是獲得四大域中的數據,格式EL表達式EL獲得pageContext域中的值:{EL表達式} EL獲得pageContext域中的值:EL表達式EL獲得pageContext域中的值:{pageScope.key};
EL獲得request域中的值:requestScope.key;EL獲得session域中的值:{requestScope.key}; EL獲得session域中的值:requestScope.key;EL獲得session域中的值:{sessionScope.key};
EL獲得application域中的值:applicationScope.key;EL從四個域中獲得某個值{applicationScope.key}; EL從四個域中獲得某個值applicationScope.key;EL從四個域中獲得某個值{key};
—同樣是依次從pageContext域,request域,session域,application域中 獲取屬性,在某個域中獲取后將不在向后尋找
1)獲得普通字符串
2)獲得User對象的值
3)獲得List的值
代碼如下:
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ page import="beyond.domain.*" %>
<%@ page import="java.util.*" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body><!-- 模擬域中的數據 --><%//存儲一個字符串request.setAttribute("company","beyond諺語" );//存儲一個對象User user = new User();user.setId(1);user.setName("siqi");user.setPassword("wsq");session.setAttribute("user", user);//存儲一個集合List<User> list = new ArrayList<User>();User user1 = new User();//list集合中的第一個元素user1.setId(2);user1.setName("qibao");user1.setPassword("wsq");list.add(user1);//list集合中的第二個元素User user2 = new User();user2.setId(3);user2.setName("yanyu");user2.setPassword("wsq");list.add(user2);application.setAttribute("list", list);%><!-- 腳本(jsp)的方式取出request域中的值(beyond諺語) --><%=request.getAttribute("company") %><!-- 腳本(jsp)的方式取出User對象中的Name(siqi)中的值 --><%User sessionUser = (User) session.getAttribute("user");out.write(sessionUser.getName());%><hr/><!-- 使用EL表達式獲得request域中的值(beyond諺語) -->${requestScope.company}<!-- 使用EL表達式獲得User對象中的Name中的值(siqi) -->${sessionScope.user.name}<!-- 使用EL表達式獲得 application域中的第二個元素的name(yanyu) -->${applicationScope.list[1].name}<hr> <!-- 使用el表達式 全域查找 也就是把域給去掉即可-->${company}${user.name}${list[1].name}</body>
</html>
package beyond.domain;public class User {private int id;private String name;private String password;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 String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
執行結果:
3.EL的內置對象11個
${pageContext.request.contextPath}:動態獲取web應用
pageScope,requestScope,sessionScope,applicationScope
---- 獲取JSP中域中的數據
param,paramValues - 接收參數.
相當于request.getParameter() request.getParameterValues()
header,headerValues - 獲取請求頭信息
相當于request.getHeader(name)
initParam - 獲取全局初始化參數
相當于this.getServletContext().getInitParameter(name)
cookie - WEB開發中cookie
相當于request.getCookies()—cookie.getName()—cookie.getValue()
pageContext - WEB開發中的pageContext.
pageContext獲得其他八大對象
${pageContext.request.contextPath}
相當于
<%=pageContext.getRequest().getContextPath%> 這句代碼不能實現
獲得WEB應用的名稱
//form.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">
<link href="${pageContext.request.contextPath}/yy.css"><!-- 這樣的地址叫做 客戶端地址 -->
<script type="text/javascript" src="${pageContext.request.contextPath}/yyy.js" ></script><!-- 這樣的地址叫做 客戶端地址 -->
<title>Insert title here</title>
</head>
<body><form action="${pageContext.request.contextPath}/el/form2.jsp" method="post"><%-- 只要是客戶端地址 建議都要把web應用名稱給寫上:${pageContext.request.contextPath} --%><input type="text" name="username"><br><input type="password" name="password"><br><input type="checkbox" name="hobby" value="zq">足球<input type="checkbox" name="hobby" value="pq">排球<input type="checkbox" name="hobby" value="ppq">乒乓球<br><input type="submit" value="提交"><br></form><img alt="" src="${pageContext.request.contextPath}/1.jpg"><!-- 這樣的地址叫做 客戶端地址 --><img alt="" src="${pageContext.request.contextPath}/2.jpg"><!-- 這樣的地址叫做 客戶端地址 --><img alt="" src="${pageContext.request.contextPath}/1.jpg"><!-- 這樣的地址叫做 客戶端地址 --><!-- <img alt="" src="1.jpg">這樣的地址叫 相對地址,跳轉的時候偶爾會出現問題 -->
</body>
</html><%-- 一個發出5次請求:
第一次:訪問該資源,服務器返回該資源全部代碼,客戶端開始接受并解析
第二次:當客戶端解析到<link href="${pageContext.request.contextPath}/yy.css"><!-- 這樣的地址叫做 客戶端地址 -->的時候,開始向服務器請求數據
第三次:<script type="text/javascript" src="${pageContext.request.contextPath}/yyy.js" ></script><!-- 這樣的地址叫做 客戶端地址 -->,向服務器請求數據
第四次:<img alt="" src="${pageContext.request.contextPath}/1.jpg"><!-- 這樣的地址叫做 客戶端地址 -->向服務器請求數據,并且緩存該圖片
第五次:<img alt="" src="${pageContext.request.contextPath}/2.jpg"><!-- 這樣的地址叫做 客戶端地址 -->向服務器請求數據,并且緩存該圖片
當訪問<img alt="" src="${pageContext.request.contextPath}/1.jpg"><!-- 這樣的地址叫做 客戶端地址 -->的的時候,發現本客戶端有該圖片緩存不用向服務器請求了 --%>
//cookie.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><%Cookie cookie = new Cookie("name","beyond");response.addCookie(cookie);//將cookie寫到客戶端%>
</body>
</html>
//form2.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><!-- 獲得表單的參數 --><%request.getParameter("username");request.getParameter("password");request.getParameter("hobby");%><!-- 使用el獲得參數 內置對象.需要獲取的數據 -->${param.username}<!-- 獲取到username(beyond)并輸出到頁面上 -->${header.Host}${header["User-Agent"]}<!-- 因為這里的User-Agent有-特殊符號,所以得使用[] -->${header["Host"]}<!-- 能用.操作的都可以用[""]操作 -->${initParam.beyond.value}${cookie.name.value}<!-- 訪問cookie的值(beyond) ,在頁面中獲得value的值--><!-- 通過el表達式獲得request對象 其中requestScope代表域-->${pageContext.request}<!-- pageContext可以獲得其中八大對象 --></body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>WEB18</display-name><!-- 定義全局初始化參數 --><context-param><param-name>beyond</param-name><param-value>wsq</param-value></context-param><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list>
</web-app>
4.EL執行表達式
例如:
${1+1}
${empty user}
${user==null?true:false}
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ page import="beyond.domain.*" %>
<%@ page import="java.util.*" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body><!-- 模擬域中的數據 --><%pageContext.setAttribute("company","ScriptKiddie" );//存儲一個字符串request.setAttribute("company","beyond諺語" );//存儲一個對象User user = new User();user.setId(1);user.setName("siqi");user.setPassword("wsq");session.setAttribute("user", user);//存儲一個集合List<User> list = new ArrayList<User>();User user1 = new User();//list集合中的第一個元素user1.setId(2);user1.setName("qibao");user1.setPassword("wsq");list.add(user1);//list集合中的第二個元素User user2 = new User();user2.setId(3);user2.setName("yanyu");user2.setPassword("wsq");list.add(user2);application.setAttribute("list", list);%><!-- 腳本(jsp)的方式取出request域中的值(beyond諺語) --><%=request.getAttribute("company") %><!-- 腳本(jsp)的方式取出User對象中的Name(siqi)中的值 --><%User sessionUser = (User) session.getAttribute("user");out.write(sessionUser.getName());%><hr/><!-- 使用EL表達式獲得request域中的值(beyond諺語) -->${requestScope.company}<!-- 使用EL表達式獲得User對象中的Name中的值(siqi) -->${sessionScope.user.name}<!-- 使用EL表達式獲得 application域中的第二個元素的name(yanyu) -->${applicationScope.list[1].name}<hr> <!-- 使用el表達式 全域查找 也就是把域給去掉即可-->${company}${user.name}${list[1].name}<!-- el可執行表達式的運算 -->${1+1}<!-- 2 -->${1==1?false:true}<!-- false --><!-- empty 判定某個對象是否是null 如果是null返回true;不是返回false -->${empty list}<!-- 不為null,返回false -->${empty user}<!-- 不為null,返回false --></body>
</html>