JSP EL部分
一、JSP EL的運算符
類型 |
定義 |
算術型 |
+ - * / div % mod |
邏輯型 |
and && or || not ! |
關係型 |
== eq != ne > gt < lt >= ge <= le |
條件型 |
a?b:c |
空 |
empty |
二、JSP EL的基本用法
類型 |
實例 |
基本調用方法 |
JavaBeans |
${user.username}
${user["username"]}
${user['username']} |
user.getUsername() |
數組 |
${sport[1]}
${sport["1"]}
${sport['1']} |
sport[1] |
List |
${phone[2]}
${phone["2"]}
${phone['2']} |
phone.get(2) |
Map |
${phone.home}
${phone["home"]}
${phone['home']} |
phone.get("home") |
三、JSP EL的內容對象
pageContext 當前頁面上下文件對象
pageScope page對象
requestScope request對象
sessionScope session對象
applicationScope application對象
param 得到頁面傳來的參數
paramValues 得到頁面傳來的多個參數,返回一個數組
header 獲取頭信息
headerValues 獲取頭信息的值
cookie 獲取cookie對象的值
initParam 獲取設定初始的參數值
pageScope page對象
requestScope request對象
sessionScope session對象
applicationScope application對象
param 得到頁面傳來的參數
paramValues 得到頁面傳來的多個參數,返回一個數組
header 獲取頭信息
headerValues 獲取頭信息的值
cookie 獲取cookie對象的值
initParam 獲取設定初始的參數值
例:
<%=session.getAttribute("phone")%>
待價於${sessionScope.phone}
待價於${sessionScope.phone}
四、如何設置JSP不使用JSP EL
1、當前頁面不要用JSP EL
<%@page isELIgnored="true" %>
2、整個web應用都不使用EL,修改web.xml文件
<web-app...>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>
</web-app...>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>
</web-app...>
五、實例
1、基本運算符的實例
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'elExample1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- 以下爲JSP EL的算術運算實例 -->
${10+10 }<br>
${10-10 }<br>
${10*10 }<br>
${10/10 }<br>
${10 div 10 }<br>
${10%10 }<br>
${10 mod 10 }<br>
<!-- 以下爲想輸入原樣的表達式,需要用\或者'進行轉義 -->
\${10+10 }<br>
'$'{10+10 }<br>
<!-- 以下爲JSP EL的關係運算實例 -->
${100>200 }<br>
${100 gt 200 }<br>
${100<200 }<br>
${100 lt 200 }<br>
${100>=200 }<br>
${100 ge 200 }<br>
${100<=200 }<br>
${100 le 200 }<br>
${100==200 }<br>
${100 eq 200 }<br>
${100 !=200 }<br>
${100 ne 200 }<br>
<!-- 以下爲比較字符,字符用單引號,字符串用雙引號引起 -->
${'e' eq 'h' }<br>
${"hit" > "him" }<br>
<!-- 以下爲邏輯運算符的實例 -->
${(10>2) && (34>25) }<br>
${(10>2) and (34>25) }<br>
${(10>2) || (34>25) }<br>
${(10>2) or (34>25) }<br>
${!(10>2)}<br>
${not(10>2)}<br>
<!-- empty運算符的應用 empty判斷時,若對象爲""或是null,則都爲true-->
<%
pageContext.setAttribute("username",null);
pageContext.setAttribute("password","");
pageContext.setAttribute("city","北京");
pageContext.setAttribute("date",new java.util.Date());
%>
<!-- 判斷username變量是否爲空,以下返回true-->
${empty username }<br>
<!-- 判斷password變量是否爲空,以下返回true -->
${empty password }<br>
<!-- 判斷city變量是否爲空,以下返回false-->
${empty city }<br>
<!-- 判斷date變量是否爲空,以下返回false -->
${empty date }<br>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'elExample1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- 以下爲JSP EL的算術運算實例 -->
${10+10 }<br>
${10-10 }<br>
${10*10 }<br>
${10/10 }<br>
${10 div 10 }<br>
${10%10 }<br>
${10 mod 10 }<br>
<!-- 以下爲想輸入原樣的表達式,需要用\或者'進行轉義 -->
\${10+10 }<br>
'$'{10+10 }<br>
<!-- 以下爲JSP EL的關係運算實例 -->
${100>200 }<br>
${100 gt 200 }<br>
${100<200 }<br>
${100 lt 200 }<br>
${100>=200 }<br>
${100 ge 200 }<br>
${100<=200 }<br>
${100 le 200 }<br>
${100==200 }<br>
${100 eq 200 }<br>
${100 !=200 }<br>
${100 ne 200 }<br>
<!-- 以下爲比較字符,字符用單引號,字符串用雙引號引起 -->
${'e' eq 'h' }<br>
${"hit" > "him" }<br>
<!-- 以下爲邏輯運算符的實例 -->
${(10>2) && (34>25) }<br>
${(10>2) and (34>25) }<br>
${(10>2) || (34>25) }<br>
${(10>2) or (34>25) }<br>
${!(10>2)}<br>
${not(10>2)}<br>
<!-- empty運算符的應用 empty判斷時,若對象爲""或是null,則都爲true-->
<%
pageContext.setAttribute("username",null);
pageContext.setAttribute("password","");
pageContext.setAttribute("city","北京");
pageContext.setAttribute("date",new java.util.Date());
%>
<!-- 判斷username變量是否爲空,以下返回true-->
${empty username }<br>
<!-- 判斷password變量是否爲空,以下返回true -->
${empty password }<br>
<!-- 判斷city變量是否爲空,以下返回false-->
${empty city }<br>
<!-- 判斷date變量是否爲空,以下返回false -->
${empty date }<br>
</body>
</html>
2、用JSP EL讀取JavaBean中的值
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.util.*,com.meixin.beans.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'elExample1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- 使用User Bean,設置屬性值username -->
<jsp:useBean id="user" class="com.meixin.beans.User"></jsp:useBean>
<jsp:setProperty name="user" property="username" value="meixin"/>
<%
//建立Profile對象,設置郵件地址
Profile p = new Profile();
p.setEmail("[email protected]");
//將不同的電話存入Map中,並設置在p對象的屬性中
Map<String,String> phone = new HashMap<String,String>();
phone.put("office","8383838");
p.setPhone(phone);
//建立地址對象,設置城市名
Address address = new Address();
address.setCity("北京");
Address[] addresses = {address};
p.setAddress(addresses);
user.setProfile(p);
%>
<!-- 用JSP EL的級連方式輸入值 -->
<!-- 輸出user對象中的username屬性值,三種寫法等價 -->
${user.username }<br>
${user["username"] }<br>
${user['username'] }<br>
<!-- 輸出user對象中profile屬性對象中的phone屬性Map中鍵值爲office的值 -->
${user.profile.phone.office }<br>
${user['rofile']['phone']['office'] }<br>
<!-- 輸出user對象中profile屬性對象中address數據屬性中第0個元素對象中的city的屬性值 -->
${user.profile.address[0].city }<br>
</body>
</html>
<%@ page import="java.util.*,com.meixin.beans.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'elExample1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- 使用User Bean,設置屬性值username -->
<jsp:useBean id="user" class="com.meixin.beans.User"></jsp:useBean>
<jsp:setProperty name="user" property="username" value="meixin"/>
<%
//建立Profile對象,設置郵件地址
Profile p = new Profile();
p.setEmail("[email protected]");
//將不同的電話存入Map中,並設置在p對象的屬性中
Map<String,String> phone = new HashMap<String,String>();
phone.put("office","8383838");
p.setPhone(phone);
//建立地址對象,設置城市名
Address address = new Address();
address.setCity("北京");
Address[] addresses = {address};
p.setAddress(addresses);
user.setProfile(p);
%>
<!-- 用JSP EL的級連方式輸入值 -->
<!-- 輸出user對象中的username屬性值,三種寫法等價 -->
${user.username }<br>
${user["username"] }<br>
${user['username'] }<br>
<!-- 輸出user對象中profile屬性對象中的phone屬性Map中鍵值爲office的值 -->
${user.profile.phone.office }<br>
${user['rofile']['phone']['office'] }<br>
<!-- 輸出user對象中profile屬性對象中address數據屬性中第0個元素對象中的city的屬性值 -->
${user.profile.address[0].city }<br>
</body>
</html>
以下爲對象的JavaBean的內容
1)Profile類
package com.meixin.beans;
import java.util.Date;
import java.util.Map;
public class Profile
{
private String email;
private Date birthday;
private Address[] address;
private Map<String, String> phone;
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
public Address[] getAddress()
{
return address;
}
public void setAddress(Address[] address)
{
this.address = address;
}
public Map<String, String> getPhone()
{
return phone;
}
public void setPhone(Map<String, String> phone)
{
this.phone = phone;
}
}
import java.util.Date;
import java.util.Map;
public class Profile
{
private String email;
private Date birthday;
private Address[] address;
private Map<String, String> phone;
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
public Address[] getAddress()
{
return address;
}
public void setAddress(Address[] address)
{
this.address = address;
}
public Map<String, String> getPhone()
{
return phone;
}
public void setPhone(Map<String, String> phone)
{
this.phone = phone;
}
}
2)User類
package com.meixin.beans;
public class User
{
private Long userID;
private String userName;
private String password;
private Profile profile;
public Long getUserID()
{
return userID;
}
public void setUserID(Long userID)
{
this.userID = userID;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public Profile getProfile()
{
return profile;
}
public void setProfile(Profile profile)
{
this.profile = profile;
}
}
public class User
{
private Long userID;
private String userName;
private String password;
private Profile profile;
public Long getUserID()
{
return userID;
}
public void setUserID(Long userID)
{
this.userID = userID;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public Profile getProfile()
{
return profile;
}
public void setProfile(Profile profile)
{
this.profile = profile;
}
}
3)Address類
package com.meixin.beans;
public class Address
{
private String city;
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city = city;
}
}
public class Address
{
private String city;
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city = city;
}
}
3、實例:輸出頁面不同範圍內屬性的值
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'elExample1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
pageContext.setAttribute("username","meixin");
request.setAttribute("username","meixinRequest");
session.setAttribute("username","meixinSession");
application.setAttribute("username","meixinApplication");
%>
<!-- 輸出meixin -->
${pageScope.username }<br>
${pageScope['username'] }<br>
<!-- 輸出值爲meixinSession -->
${sessionScope.username }<br>
<!-- 輸出值爲meixinRequest -->
${requestScope.username }<br>
<!-- 輸出值爲meixinApplication -->
${applicationScope.username }<br>
<!-- 輸出值爲meixin,此變量系統根據pageContext,request,session,application依次查找 -->
${username }<br>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'elExample1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
pageContext.setAttribute("username","meixin");
request.setAttribute("username","meixinRequest");
session.setAttribute("username","meixinSession");
application.setAttribute("username","meixinApplication");
%>
<!-- 輸出meixin -->
${pageScope.username }<br>
${pageScope['username'] }<br>
<!-- 輸出值爲meixinSession -->
${sessionScope.username }<br>
<!-- 輸出值爲meixinRequest -->
${requestScope.username }<br>
<!-- 輸出值爲meixinApplication -->
${applicationScope.username }<br>
<!-- 輸出值爲meixin,此變量系統根據pageContext,request,session,application依次查找 -->
${username }<br>
</body>
</html>
4、實例:param用於獲取上一頁面傳遞的參數值
<!-- param用於獲取上一頁面傳遞來的參數值-->
${param.username}<br>
${param.password}<br>
${param.username}<br>
${param.password}<br>
5、實例:cookie用於獲取cookie參數的值
<%
response.addCookie(new Cookie("username","meixin"));
%>
<!-- 輸出cookie中user的值,此處輸出meixin -->
${cookie.user.value }
response.addCookie(new Cookie("username","meixin"));
%>
<!-- 輸出cookie中user的值,此處輸出meixin -->
${cookie.user.value }
6、實例:initParam用於獲取web.xml中初始的參數值
1)web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 這裏context-param標記中設置初始參數repeat的值爲100 -->
<context-param>
<param-name>repeat</param-name>
<param-value>100</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 這裏context-param標記中設置初始參數repeat的值爲100 -->
<context-param>
<param-name>repeat</param-name>
<param-value>100</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2)JSP EL代碼
${initParam.repeat}