11 个隐含对象
EL 表达式中有 11 个隐含对象,是 EL 表达式中自己定义的,可以直接使用。
-
变量 类型 作用 pageContext PageContextImpl 它可以获取 jsp 中的九大内置对象 pageScope Map<String,Object> 它可以获取 pageContext 域中的数据 requestScope Map<String,Object> 它可以获取 Request 域中的数据 sessionScope Map<String,Object> 它可以获取 Session 域中的数据 applicationScope Map<String,Object> 它可以获取 ServletContext 域中的数据 param Map<String,String> 它可以获取请求参数的值 paramValues Map<String,String[]> 它也可以获取请求参数的值,获取多个值的时候使用。 header Map<String,String> 它可以获取请求头的信息 headerValues Map<String,String[]> 它可以获取请求头的信息,它可以获取多个值的情况 cookie Map<String,Cookie> 它可以获取当前请求的 Cookie 信息 initParam Map<String,String> 它可以获取在 web.xml 中配置的<context-param>上下文参数
i. EL 获取四个特定域中的属性
pageScope ====== pageContext 域
requestScope ====== Request 域
sessionScope ====== Session 域
applicationScope ====== ServletContext 域
示例:
<body>
<%
pageContext.setAttribute("key","pageContext");
request.setAttribute("key","request");
session.setAttribute("key","session");
application.setAttribute("key","application");
%>
${pageScope.key}<br/><%--pageContext--%>
${requestScope.key}<br/><%--request--%>
</body>
ii. pageContext 对象的使用
- 协议:
- 服务器 ip:
- 服务器端口:
- 获取工程路径:
- 获取请求方法:
- 获取客户端 ip 地址:
- 获取会话的 id 编号
示例:
<body>
<%--
request.getScheme() 它可以获取请求的协议
request.getServerName() 获取请求的服务器ip或域名
request.getServerPort() 获取请求的服务器端口号
getContextPath() 获取当前工程路径
request.getMethod() 获取请求的方式(GET或POST
request.getRemoteHost() 获取客户端的ip 地址
session.getId() 获取会话的唯一标识
--%>
<%
pageContext.setAttribute("req",request);
%>
<%=request.getScheme()%><br/>
1. 协议:${req.scheme}<br/>
2. 服务器 ip:${pageContext.request.serverName}<br/>
3. 服务器端口:${pageContext.request.serverPort}<br/>
4. 获取工程路径:${pageContext.request.contextPath}<br/>
5. 获取请求方法:${pageContext.request.method}<br/>
6. 获取客户端 ip 地址:${pageContext.request.remoteHost}<br/>
7. 获取会话的 id 编号:${pageContext.session.id};
</body>
iii. EL 表达式其他隐含对象的使用
- param Map<String,String> 它可以获取请求参数的值
- paramValues Map<String,String[]> 它也可以获取请求参数的值,获取多个值的时候使用。
示例代码:
输出请求参数password的值:${ param.password } <br>
输出请求参数username的值:${ paramValues.username[0] } <br>
输出请求参数hobby的值:${ paramValues.hobby[0] } <br>
输出请求参数hobby的值:${ paramValues.hobby[1] } <br>
请求地址:http://localhost:8080/10_EL/otherparam.jsp?username=zhangsan&username=lisi&password=123&hobby=dance&hobby=sing
结果:
- header Map<String,String> 它可以获取请求头的信息
- headerValues Map<String,String[]> 它可以获取请求头的信息,它可以获取多个值的情况
示例代码:
<%-- 含特殊符号的要用中括号括起来--%>
输出请求头【User-Agent】的值:${header['User-Agent']}<br/>
输出请求头【Connection】的值:${header.Connection}<br/>
输出请求头【User-Agent】的值:${headerValues['User-Agent'][0]}<br/>
结果:
- cookie Map<String,Cookie> 它可以获取当前请求的 Cookie 信息
示例代码:
获取Cookie的名称:${cookie.JSESSIONID.name}<br>
获取Cookie的值:${cookie.JSESSIONID.value} <br>
结果:
- initParam Map<String,String> 它可以获取在 web.xml 中配置的上下文参数
web.xml 中的配置:
<context-param>
<param-name>username</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<!-- ///表示localhost:3306-->
<param-value>jdbc:mysql:///bjpowernode</param-value>
</context-param>
注意:改变了web.xml配置文件一定要重新部署工程
示例代码:
输出<Context-param>username的值:${initParam.username} <br>
输出<Context-param>url的值:${initParam.url}<br>
结果: