转载:http://wnight88.blog.51cto.com/512204/163189
一、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("wnight88@sina.com");
//将不同的电话存入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("wnight88@sina.com");
//将不同的电话存入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}
相关推荐
【JSP(Java Server ...在"毕业设计用读书笔记(jsp)"的压缩包中,可能包含了作者在学习JSP过程中整理的资料,包括对各个概念的理解、代码示例、问题解析等,对于正在做毕业设计的同学来说,是一份宝贵的参考资料。
【韩顺平jsp九讲笔记】是一份详细记录了韩顺平老师关于JSP(Java Server Pages)技术的九次讲解的学习资料。这份笔记涵盖了JSP的基础知识、核心概念以及实际应用,旨在帮助学习者深入理解和掌握JSP编程。韩顺平老师...
- **EL**是JSP 2.0引入的一种轻量级的脚本语言,用于简化JSP中的数据访问,它可以用来获取和设置JavaBean或其他作用域内的对象属性。 综上所述,JSP是Web开发中的重要技术,结合EL能更高效地处理动态内容。理解JSP...
### JSP与JSTL及EL表达式的综合解析 #### JSP概述 JSP(Java Server Pages)是一种基于Java技术的动态网页开发技术,它允许将动态内容嵌入到静态HTML页面中。JSP文件本质上是Servlet的一个特殊形式,但它们以更简洁...
【JSP实用教程笔记】 JSP(JavaServer Pages)是一种基于Java技术的动态网页开发工具,它允许开发者在HTML页面中嵌入Java代码,实现服务器端的动态网页生成。本笔记将围绕“jsp实用教程”这本书的核心内容进行展开...
**新一代JSP网络编程笔记** 本笔记主要涵盖了JavaServer Pages(JSP)技术在网络编程中的应用,旨在帮助读者深入理解JSP的核心概念、语法特点以及实际开发中的最佳实践。JSP是Java平台上的一个标准,它允许开发人员...
【标题】"jsp自制详细笔记"揭示了这个压缩包文件主要涵盖了JavaServer Pages(JSP)和Servlet技术的学习资源,特别适合JavaWeb开发的初学者。JSP是一种动态网页技术,它允许开发者在HTML页面中嵌入Java代码,从而...
在IT行业中,EL(Expression Language)是Java EE平台的一个重要组成部分,主要用于简化JSP(JavaServer Pages)和其他Java EE组件中的表达式处理。EL提供了一种简洁的语法,用于访问JavaBeans或者其他Java对象的...
在学习过程中,阅读提供的"jsp 基础教程.doc", "jsp学习 笔记.doc", "JDBC 笔记.doc", "jsp 笔记二.doc"文档,将有助于你深入理解JSP和JDBC的使用方法和最佳实践。这些笔记详细阐述了相关概念、示例代码和常见问题,...
本学习笔记源码提供了全面的学习资源,涵盖了这两个技术的基础到高级应用,旨在帮助开发者深入理解并熟练掌握JSP和Servlet。 1. **JSP基础**: JSP是一种服务器端的脚本语言,它将HTML、CSS、JavaScript与Java代码...
除了上述基础语法,JSP还支持EL(Expression Language)和JSTL(JavaServer Pages Standard Tag Library),它们提供了更简洁的表达式和标准标签库,使JSP代码更加简洁和易读。 在实际开发中,通常推荐使用JSP的...
2.jsp运行原理-----jsp本质就是servlet(面试) jsp在第一次被访问时会被Web容器翻译成servlet,在执行 过程: 第一次访问---->helloServlet.jsp---->helloServlet_jsp.java---->编译运行 PS:被翻译后的...
根据提供的文件信息,这里将基于“jsp考试笔记”的标题、描述及部分难以辨识的内容来构建一份关于JSP(JavaServer Pages)的知识点总结。 ### JSP基础概念 #### 定义 JSP(JavaServer Pages)是一种由Sun ...
- `isELIgnored`:控制是否忽略Expression Language(EL)。 9. **注意事项**:确保页面输出的字符集与接收请求时的编码一致,如通过`request.setCharacterEncoding()`设置POST请求的编码。如果需要全局处理编码...
**JSP 2.0 学习笔记及完整源码详解** JSP 2.0(JavaServer Pages)是Java平台上的动态网页技术,它允许开发者在HTML、XML或其他标记语言文档中嵌入Java代码,以实现服务器端的动态内容生成。本资料包包括了JSP 2.0...
由于这部分内容是电子书中的学习笔记,电子书的内容并未直接给出,因此只能根据电子书的标题“JSP&Servlet学习笔记”和描述来推测可能涉及的知识点,而没有具体的电子书内容可以分析。如需进一步学习,建议查阅相关...
5. **EL(Expression Language)**:它是JSP 2.0引入的一种轻量级表达式语言,用于访问JavaBean或其他Java对象的属性。 6. **JSTL(JavaServer Pages Standard Tag Library)**:一套标准的标签库,提供诸如流程...