EL表达式域内置对象和页面上下文隐藏对象
---------------------------------------- EL表达式域内置对象和页面上下文隐藏对象--------------------------------
域隐藏对象
使用EL表达式最为常用的就是获取域对象中保存的数据。例如:${pageScope.xxx},表示获取在pageContext保存的数据。当然${pageScope[‘xxx’]}是相同的!
- pageScope:pageScope是Map<String,Object>类型,${pageScope.xxx}的功能相等与pageContext.getAttribute(“xxx”)。两者的区别在于,前者在数据不存在时返回空字符串,而后者返回null。
- requestScope:requestScope是Map<String, Object>类型,装载了request对象中的所有数据;
- sessionScope:sessionScope是Map<String, Object>类型,装载了session对象中的所有数据;
- applicationScope:applicationScope是Map<String, Object>类型,装载了application对象中的所有数据;
当EL中给出的不是隐藏对象时,表示在四个域中查找数据。例如:${a},表示
- 在${pageScope.a}中查找,如果找到就返回;
- 在${requestScope}中查找,如果找到就返回;
- 在${sessionScope}中查找,如果找到就返回;
- 在${applicationScope}中查找,如果找到就返回,找不到就返回空字符串。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="com.rl.model.*" %> <!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>Insert title here</title> </head> <body> <% Person person = new Person(); person.setAge(10); person.setId(1); person.setName("likunpeng"); pageContext.setAttribute("person", person); request.setAttribute("person", person); session.setAttribute("person", person); application.setAttribute("person", person); %>
<% Person p = (Person)pageContext.getAttribute("person"); Person p1 = (Person)request.getAttribute("person"); Person p2 = (Person)session.getAttribute("person"); Person p3 = (Person)application.getAttribute("person"); %>
<%=p %><br> <%=p.getName() %><br> <%=p.getAge() %><br> <%=p.getId() %><br> <hr> <h1>EL表达式从pageContext域中来取值</h1> <h2>${pageScope.person.id }</h2> <h2>${pageScope.person.name }</h2> <h2>${pageScope.person.age }</h2> <hr> <h1>EL表达式从request域中来取值</h1> <h2>${requestScope.person.id }</h2> <h2>${requestScope.person.name }</h2> <h2>${requestScope.person.age }</h2> <hr> <h1>EL表达式从session域中来取值</h1> <h2>${sessionScope.person.id }</h2> <h2>${sessionScope.person.name }</h2> <h2>${sessionScope.person.age }</h2> <hr> <h1>EL表达式从application域中来取值</h1> <h2>${applicationScope.person.id }</h2> <h2>${applicationScope.person.name }</h2> <h2>${applicationScope.person.age }</h2> <hr> <h1>EL表达式默认从域中来取值</h1> <%-- 默认情况会优先去最小的域中去寻找 --%> <h2>${person.id }</h2> <h2>${person.name }</h2> <h2>${person.age }</h2> </body> </html> |
3 页面上下文隐藏对象
- pageContext:pageContext是PageContext类型!可以使用pageContext对象调用getXXX()方法,例如pageContext.getRequest,可以${pageContext.request}。
Expression |
说明 |
${pageContext.request.queryString} |
pageContext.getRequest().getQueryString(); |
${pageContext.request.requestURL} |
pageContext.getRequest().getRequestURL(); |
${pageContext.request.contextPath} |
pageContext.getRequest().getContextPath(); |
${pageContext.request.method} |
pageContext.getRequest().getMethod(); |
${pageContext.request.protocol} |
pageContext.getRequest().getProtocol(); |
${pageContext.request.remoteUser} |
pageContext.getRequest().getRemoteUser(); |
${pageContext.request.remoteAddr} |
pageContext.getRequest().getRemoteAddr(); |
${pageContext.session.new} |
pageContext.getSession().isNew(); |
${pageContext.session.id} |
pageContext.getSession().getId(); |
${pageContext.servletContext.serverInfo} |
pageContext.getServletContext().getServerInfo(); |