表达式语言

表达式语言的内置对象
NO.表达式内置对象说明
1pageContext表示javax.servlet.jsp.PageContext
2pageScope表示从page属性范围查找输出属性
3requestScope表示从request属性范围查找输出属性
4sessionScope表示从session属性范围查找输出属性
5applicationScope表示从application属性范围查找输出属性
6param接收传递到本页面的参数
7paramValues接收传递到本页面的一组参数
8header取得一个头信息数据
9headerValues取出一组头信息数据
10cookie取出cookie中的数据
11initParam取得配置的初始化参数

使用表达式语言可以输出4中属性范围中的内容,如果此时在不同的属性范围中设置了同一个属性名称,则将按照如下顺序查找:
page→request→session→application
属性范围
NO.属性范围范例说明
1pageScope${pageScope.属性}取出page范围的属性内容
2requestScope${requestScope.属性}取出request范围的属性内容
3sessionScope${sessionScope.属性}取出session范围的属性内容
4applicationScope${applicationScope.属性}取出application范围的属性内容
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title></title></head>
<body>
<%
	pageContext.setAttribute("info","page属性范围") ;
	request.setAttribute("info","request属性范围") ;
	session.setAttribute("info","session属性范围") ;
	application.setAttribute("info","application属性范围") ;
%>
<h3>PAGE属性内容:${pageScope.info}</h3>
<h3>REQUEST属性内容:${requestScope.info}</h3>
<h3>SESSION属性内容:${sessionScope.info}</h3>
<h3>APPLICATION属性内容:${applicationScope.info}</h3>
</body>
</html>

使用pageContext对象可以取得request、session、application的实例,所以在表达式语言中,可以通过pageContext这个表达式的内置对象调用JSP内置对象中提供的方法。
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title></title></head>
<body>
<h3>IP地址:${pageContext.request.remoteAddr}</h3>
<h3>SESSION ID:${pageContext.session.id}</h3>
<h3>是否是新session:${pageContext.session.new}</h3>
</body>
</html>

接收请求参数:
${param.参数名称}
<span style="font-size:14px;"><%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title></title></head>
<body>
<h3>通过内置对象接收输入参数:<%=request.getParameter("ref")%></h3>
<h3>通过表达式语言接收输入参数:${param.ref}</h3>
</body>
</html></span>
接收一组参数:
${paramValues.参数名称}
<html>
<head><title></title></head>
<body>
<form action="param_values_demo.jsp" method="post">
	兴趣:	<input type="checkbox" name="inst" value="唱歌">唱歌
			<input type="checkbox" name="inst" value="游泳">游泳
			<input type="checkbox" name="inst" value="看书">看书
			<input type="submit" value="显示">
</form>
</body>
</html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title></title></head>
<body>
<%	// 正经开发中,此代码要通过过滤器实现
	request.setCharacterEncoding("GBK") ;
%>
<h3>第一个参数:${paramValues.inst[0]}</h3>
<h3>第二个参数:${paramValues.inst[1]}</h3>
<h3>第三个参数:${paramValues.inst[2]}</h3>
</body>
</html>
集合操作:
List集合
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title></title></head>
<body>
<%
	List all = new ArrayList() ;
	all.add("李兴华") ;
	all.add("www.MLDNJAVA.cn") ;
	all.add("mldnqa@163.com") ;
	request.setAttribute("allinfo",all) ;	// 集合保存在request范围
%>
<h3>第一个元素:${allinfo[0]}</h3>
<h3>第二个元素:${allinfo[1]}</h3>
<h3>第三个元素:${allinfo[2]}</h3>
</body>
</html>
Map集合
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title></title></head>
<body>
<%
	Map map = new HashMap() ;
	map.put("lxh","李兴华") ;
	map.put("mldn","www.MLDNJAVA.cn") ;
	map.put("email","mldnqa@163.com") ;
	request.setAttribute("info",map) ;	// 集合保存在request范围
%>
<h3>KEY为lxh的内容:${info["lxh"]}</h3>
<h3>KEY为mldn的内容:${info["mldn"]}</h3>
<h3>KEY为email的内容:${info["email"]}</h3>
</body>
</html>
运算符
算术运算符:
NO.算术运算符描述范例结果
1+加法操作${20+30}50
2-减法操作${20-30}-10
3*乘法操作${20*30}600
4/或div除法操作${20/30}或${20 div 30}0.666
5%或mod取模(余数)${20%30}或${20 mod 30}20
关系运算符:
NO.关系运算符描述范例结果
1==或eq等于${20==30}或${20 eq 30}false
2!=或ne不等于${20!=30}或${20 !ne 30}true
3<或lt小于${20<30}${20 lt 30}true
4>或gt大于${20>30}或${20 gt 30}false
5<=或le小于等于${20<=30}或${20 le30}true
6>=或ge大于等于${20>=30}或${20 ge30}false
逻辑运算符:
NO.逻辑运算符描述范例结果
1&&或and与操作${true&&false}或${true and false}false
2||或or或操作${true||false}或${true or false}true
3!或not非操作(取反)${!true}或${not true}false
其他运算符:
NO.其他运算符描述范例结果
1empty判断是否为null${empty info}true
2? :三目运算符${10>20?"大于":"小于"}小于
3()括号运算符${10*(20+30)}500




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值