el即expression language(表达式语言),是一种简单的数据访问语言。
el和输出表达式<%= %>的功能差不多,都是输出表达式的值,但二者使用的对象有所不同。
${表达式} //表达式后面不能加分号。
${1} //int
${"ok"} //string
${false} //boolean
${null} //null
${name=1} //1。el表达式中的变量不用先声明|定义,可直接使用。比如此处未声明 int name 。
${name} //访问变量的值
以java获取值,如果变量不存在或值是null,,会报空指针异常。el表达式则不会,el表达式会输出空串。
${a+b} //加
${a-b} //减
${a*b} //乘
${a/b} 或 ${a div b} //除
${a%b} 或 ${a mod b} //取模
返回boolean值,为避免与html标签<>冲突,可使用英文简写。
${a==b} 或 ${a eq b} equals
${a!=b} 或 ${a nq b} not equals
${a<b} 或 ${a lt b} less than
${a<=b} 或 ${a le b} less equals
${a>b} 或 ${a gt b} greater than
${a>=b} 或 ${a ge b} greater equals
${a && b} 或 ${a and b}
${a || b} 或 ${a or b}
${!a} 或 ${not a}
${a?b:c}
${empty a} //判断变量a是否为空,返回boolean值
若变量a不存在,返回true;
若变量a存在,但值为null,返回true;
若变量a是集合类型(list、set、map),值不为null,但却是空集合,返回true。
不确定运算符的优先级时,加()。
el表达式那么多数据都不能访问,为什么是数据访问语言?
el表达式有自己的内置对象,一般地,el表达式使用自己的内置对象来访问数据。
这些内置对象都是使用map来存储数据。
el表达式虽然不能访问<% %>中定义的变量,比如 <% student student=new student(); %> ,在el表达式中不能访问student对象。
1 <% 2 pagecontext.setattribute("id","01"); //page域 3 request.setattribute("name","chy"); //request域 4 session.setattribute("age",20); //session域 5 application.setattribute("score",100); //application域 6 %> 7 8 <%-- 2种写法等效--%> 9 ${pagescope.get("id")} 10 ${pagecontext.getattribute("id")} 11 12 ${requestscope.get("name")} 13 ${pagecontext.request.getattribute("name")} 14 15 ${sessionscope.get("age")} 16 ${pagecontext.session.getattribute("age")} 17 18 ${applicationscope.get("score")} 19 ${pagecontext.servletcontext.getattribute("score")}
这4个域只有取数据的方法,没有设置数据的setxxx()方法。
这些获取其他对象的方式,表面上是通过成员变量来获取,实际仍是调用getxxx()来获取。
pagecontext相当于一个大map,里面还装了些小map。
el内置的4个域对象没有设置数据的setxxx()方法。设置数据可以用上面pagecontext的几个成员变量来实现:
${pagecontext.request|session|servletcontext.set|remove|getattribute() }
上面只有三个域,page域呢?
pagecontext本身就是page域: pagecontext.set|get|removeattribute()
此外pagecontext还有2个常用方法:
均是服务器内部行为,浏览器总的只发送一次http请求,可以看到地址栏是没有变的。
param、paramvalues中保存的是请求参数(表单数据、url中?后面的部分)。
请求参数会存储为2份:
<form method="post" action="handle.jsp"> 姓名:<input type="text" name="name" /><br /> 爱好:<input type="checkbox" name="hobby" value="running" >跑步 <input type="checkbox" name="hobby" value="swimming" >游泳<br /> <button type="submit">提交</button> </form>
<% request.setcharacterencoding("utf-8"); response.setcontenttype("text/html;charset=utf-8"); %> <p>姓名:${param.name}</p> <p>爱好:${paramvalues.hobby[0]}、${paramvalues.hobby[1]}</p>
param是map<stirng,string>类型、paramvalues是map<stirng,string[]>类型,这2个el内置对象都可以使用map的方法。
cookie对象表示存储请求头中所有cookie的map集合。
${cookie.jsessionid} //获取jsessionid这个cookie
${cookie.jsessionid.name} //获取jsessionid这个cookie的name
${cookie.jsessionid.value} //获取jsessionid这个cookie的value
如对本文有疑问, 点击进行留言回复!!
[JVM学习之路]一、初识JVM,了解其结构、模型及生命周期
【JAVA并发编程】LinkedBlockingQueue原理
网友评论