el表達式
el: jsp的內置表達式語言,從jsp2.0開始,用來替代<%=..%>
作用: 1.獲取域中數據 ★ 2.執行運算 ★ 3.獲取常見的web對象 4.調用java的方法
格式: ${el表達式}
獲取域中數據:★ 注意:★ 若屬性名中出現了"."|"+"|"-"等特殊符號,需要使用scope獲取 例如: ${requestScope["arr.age"] } 獲取簡單數據 ${pageScope|requestScope|sessionScope|applicationScope.屬性名} 以后經常使用: ${屬性名}:依次從pageContext,request,session,application查找指定屬性,若查找到返回值,結束該次查找 若查找不到,返回"" 獲取復雜數據 獲取數組中的數據: ${域中的名稱[index]} 獲取list中的數據: ${域中的名稱[index]} 獲取map中的數據: ${域中的名稱.鍵名}
案例1--el獲取域中簡單數據
demo1.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>el獲取域中簡單數據</title>
</head>
<body>
<% request.setAttribute("rkey", "rvalue"); session.setAttribute("rkey", "svalue"); session.setAttribute("skey", "svalue"); application.setAttribute("akey", "avalue"); %> 獲取request中的數據: 老方式:<%=request.getAttribute("rkey") %><br> el方式:${requestScope.rkey }<br>
<hr> 獲取session中的數據: 老方式:<%=session.getAttribute("skey") %><br> el方式:${sessionScope.skey }<br>
<hr> 獲取application中的數據: 老方式:<%=application.getAttribute("akey") %><br> el方式:${applicationScope.akey }<br>
<hr>
<hr> 獲取application數據失敗: 老方式失敗:<%=application.getAttribute("aakey") %><br> el方式失敗:${applicationScope.aakey }<br>
<hr style="border-color:red">
<!-- request和session中都有rkey,查找的時候是先從小域到大域中查起,查找 到了就立即結束,不會再往下面查--> 便捷獲取: ${rkey },${skey },${akey },${aakey } <hr style="border-color:red">
</body>
</html>
啟動tomcat,瀏覽器中輸入以下url,回車,頁面顯示如下:
案例2--el獲取域中復雜數據
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ 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=utf-8">
<title>el獲取域中復雜數據</title>
</head>
<body>
<%
//往request域中放入數組
request.setAttribute("arr", new String[]{"aa","bb","cc"}); //往request域中放入list中
List list = new ArrayList(); list.add(111); list.add(222); list.add(333); request.setAttribute("list", list); //往request域中放入map集合中
Map map = new HashMap(); map.put("username","hjh"); map.put("password","12345"); request.setAttribute("map", map); request.setAttribute("user-age", "999"); %> 獲取域中的數組:<br> 老方式:<%=((String[])request.getAttribute("arr"))[1] %><br> el方式:${arr[1] }<br>
<hr> 獲取域中的list:<br> 老方式:<%=((List)request.getAttribute("list")).get(1) %><br> el方式:${list[1] }<br> list的長度:${list.size() } <hr> 獲取域中的map:<br> 老方式:<%=((Map)request.getAttribute("map")).get("username") %><br> el方式:${map.username }<br>
<hr> 獲取特殊名字的數據 : ${requestScope["user-age"] } </body>
</html>
啟動tomcat,瀏覽器中輸入以下url,回車,頁面顯示如下:
javabean導航 javabean: java語言編寫的一個可重用的組件, 狹義上來說就是我們編寫的一個普通的java類 例如:User Person javabean規范: 1.必須是一個公共的具體的類 public class
2.提供私有的字段 private String id;//id稱之為字段
3.提供公共訪問字段的方法 get|set|is方法 public String getId(){..} 一旦有公共的方法之后,get|set|is之后的內容,將首字母小寫,將這個東西稱之為bean屬性 id就是一個bean屬性 4.提供一個無參的構造器 5.一般實現序列化接口 serializable ${域中javabean名稱.bean屬性}
案例--javabean
user.java
package com.hjh.javabean; public class User { private String id; private String username; private String password; public User() {} public User(String id,String username,String password) { this.id = id; this.username = username; this.password = password; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return username; } public void setName(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; } }
javabean.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@page import="com.hjh.javabean.*" %>
<!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>javabean</title>
</head>
<body>
<% User user=new User(); user.setId("001"); user.setName("hjh");//name為javabean屬性,而非username
user.setPassword("12345"); request.setAttribute("user",user); %> 獲取域中javabean的id值:<br> 老方式:<%=((User)request.getAttribute("user")).getId() %><br> el方式:${user.id }<!-- 相當於getXXX() -->
<hr> el獲取name:<br> 正確演示:${user.name }<!-- 輸出:hjh -->
</body>
</html>
啟動tomcat,瀏覽器中輸入以下url,回車,頁面顯示如下:
加入以下代碼,程序報錯,應為user類中並沒有提供getUsername()、setUsername()方法:
錯誤演示:${user.username}
錯誤信息如下:
el執行運算: 四則運算 關系(>..) 邏輯(&& ||) 注意: +:只能進行加法運算,字符串形式數字可以進行加法運算. 數字+數字字符串=數字 數字字符串+數字字符串 報錯 empty:判斷一個容器的長度是否為0(array set list map),還可以判斷一個對象是否為空 ${empty 域中的對象名稱} 三元運算符
案例1--el加法運算:
<%@ 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>el加法運算</title>
</head>
<body>
<% request.setAttribute("a", 11); request.setAttribute("b", 22); request.setAttribute("c", "33"); request.setAttribute("d", "kkk"); %> 數字+數字:${a+b }<br><!-- 33 --> 數字+數字字符串:${a+c }<br><!-- 44 --> 數字字符串+數字字符串:${c+c }<br><!-- 66 -->
<!-- 以下加法報錯 --> 數字+字母字符串:${a+d}<br><!--報錯--> 數字字符串+字母字符串:${c+d }<br><!--報錯--> 字母字符串+字母字符串:${d+d }<br><!--報錯-->
</body>
</html>
案例2--el判斷長度empty:
empty1.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@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=utf-8">
<title>empty判斷長度是否為0</title>
</head>
<body>
<% List list1=null; request.setAttribute("list1", list1); List list2 = new ArrayList(); list2.add(1234); request.setAttribute("list2", list2); %> request域中lis:1的長度是否為0:${empty list1 }<br><!-- true --> request域中list2的長度是否為0:${empty list2 }<!-- false -->
</body>
</html>
啟動tomcat,瀏覽器中輸入以下url,回車,頁面顯示如下:
案例3--el判斷對象是否為empty:
empty2.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@page import="com.hjh.javabean.*" %>
<!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>empty判斷對象是否為0</title>
</head>
<body>
<% User user1 = null; request.setAttribute("user1", user1); User user2 = new User(); request.setAttribute("user2", user2); %> request域中javabean對象的長度是否為0:${empty user1 }<br><!-- true --> request域中javabean對象的長度是否為0:${empty user2 }<!-- false -->
</body>
</html>
啟動tomcat,瀏覽器中輸入以下url,回車,頁面顯示如下:
案例3--el三元運算符:
demo3.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>el獲取域中簡單數據</title>
</head>
<body>
<%
int i=3; %>
<hr> ${3>4?"yes":"no" }<br> ${i==3?"yes":"no" } <hr>
</body>
</html>
啟動tomcat,瀏覽器中輸入以下url,回車,頁面顯示如下:
el的內置對象(了解):
11個
pageScope
requestScope
sessionScope
applicationScope
param
paramValues
header
haederValues
initParam
cookie★
pageContext★
注意:
除了pagecontext其余對象獲取的全是map集合
了解:和參數相關的el內置對象
param
paramValues
案例:
<%@ 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>el內置對象param¶mValues</title>
</head>
<body> ${param.username }<br> ${param }<br> ${paramValues }<br> ${paramValues.hobby[0] }<br>
</body>
</html>
http://localhost:8080/Jsp/el/param?username=hjh&password=12345&hobby=eat&hobby=sport
了解:和請求頭相關的el內置對象
header
haederValues
案例:
header.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>el內置對象header&headerValues</title>
</head>
<body> header:${header }<br>
<hr style="border-color:red"> headerValues:${headerValues }<br>
<hr style="border-color:red"> referer:${header.referer }<br>
<hr style="border-color:red"> user-agent:${headerValues["user-agent"][0] } </body>
</html>
啟動tomcat,輸入以下url,回車,頁面顯示如下:
了解:和全局初始化參數相關的el內置對象
initParam
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"
version="3.1"> <context-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </context-param> </web-app>
initParam.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>el獲取域中簡單數據</title>
</head>
<body> ${initParam } </body>
</html>
啟動tomcat,瀏覽器中輸入以下url,回車,頁面顯示如下:
cookie內置對象: ${cookie} 獲取map{key=Cookie} 例如:創建cookie Cookie c=new Cookie("username","tom"); 通過${cookie}獲取相當於 {username=new Cookie("username","tom")} 相當於map的key是cookie的鍵 map的value是當前cookie 若項獲取名稱username的cookie的value值(獲取tom) ${cookie.username.value}--javabean導航 注意: java中Cookie的api getName():獲取cookie的名稱 getValue():獲取cookie的value值 我們稱name和value是cookie的bean屬性 使用cookie內置對象: ${cookie.給cookie起名字.value} 例如: 獲取jsession的值 ${cookie.JSESSIONID.value}
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>el加法運算</title>
</head>
<body> ${cookie.JSESSIONID.value } </body>
</html>
瀏覽器顯示如下:
pageContext:獲取不是map集合,相當於jsp的pageContext內置對象
在jsp頁面中獲取項目名
${pageContext.request.contextPath}
pageContext.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>el加法運算</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/el/text.jsp">點擊跳轉到text.jsp</a>
</body>
</html>
啟動comcat,訪問以下url,顯示一個超鏈接,如下圖:
點擊超鏈接,頁面跳轉到text.jsp頁面,url隨之改變: