问题?EL的11大隐式对象有哪些?和jsp的9大隐式对象有区别吗?
首先,EL的11大隐式对象与jsp的9大隐式对象是不同的。前者的对象只在<%%>中使用。而后者在${}表达式中使用
一、获取JSP的内置对象(11大EL内置对象):难点,不要与JSP的内置对象和范围名称搞混
11大EL隐式对象中,其中一个是表示自身对象外,其余都是表示的Map结构
EL隐式对象名称 | Java类型 | 备注 |
pageContext | javax.servlet.jsp.PageContext | 与JSP中的内置对象完全相同 |
pageScope | java.util.Map | 代表着PageContext页面范围域那个Map |
requestScope | 代表着ServletRequest请求范围域那个Map | |
sessionScope | 代表着HttpSession会话范围域那个Map | |
applicationScope | 代表着ServletContext应用范围域那个Map | |
param | 请求参数。key:请求参数的名称。value:请求参数的值,它是字符串。 | |
paramValues | 请求参数。key:请求参数的名称。value:请求参数的值,它是字符串数组。 | |
header | 代表着请求消息头。key:头名称。value:头值,它是一个字符串。 | |
headerValues | 代表着请求消息头。key:头名称。value:头值,它是一个字符串数组。 | |
cookie | 代表客户端提交的Cookie的Map。key:cookie的name。value:cookie对象本身 | |
initParam | 代表着全局初始化参数(web.xml中context-param).key:参数名称。value:参数值 |
例子:
Bean类
package com.dp.javaweb.domain;
import java.io.Serializable;
import java.util.Date;
public class bean implements Serializable{
//80%的类都实现这个接口,虽然并不是都用,但是养成良好的习惯是好的
private String username="sssss";
private String password;
private boolean marry;
public bean(String username, String password, boolean marry) {
super();
this.username = username;
this.password = password;
this.marry = marry;
}
public bean() {
// TODO Auto-generated constructor stub
}
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 boolean isMarry() {//is和get是一样的
return marry;
}
public void setMarry(boolean marry) {
this.marry = marry;
}
}
jsp
<%@page import="com.dp.javaweb.domain.bean"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP '3.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>
<h1>EL表达式的11大隐式对象(跟jsp的9大隐式对象是不同的)</h1>
<h2>pageContext内置对象</h2>
<!-- El内置对象获得HttpServletRequest对象 -->
${pageContext.request }<br/>
<!-- 通过 HttpServletRequest得到当前应用的名称-->
${pageContext.request.contextPath }<br/>
<!-- 通过EL得到 HttpServletResponse的使用编码-->
${pageContext.response.characterEncoding }<br/>
<h2>pageScope内置对象</h2>
<!--${pageScope } -->
<%
pageContext.setAttribute("p1", "p1的值");
pageContext.setAttribute("p2", new bean("Mr_Li13","男",true));
pageContext.setAttribute("p3", new bean("Mr_Li131","男",true),PageContext.REQUEST_SCOPE);
session.setAttribute("user", new bean("Mr_Li13","男",true));
%>
${pageScope.p1 }<!-- EL的11个内置对象中除了pageContext,其他都是Map集合,取得p1对象的值 -->
<br/> ${pageScope.p2.username }<!-- -->
<br/> ${requestScope.p3.username }<!--取request范围的值 -->
<br/><h3> ${empty sessionScope.user?"请登录":"欢迎您:" }${sessionScope.user.username}<h3/><!--取session范围的值,也是登陆标记常用的方法,重点 -->
<h2>param请求参数的使用</h2>
${param }<!-- 请求方式:http://localhost:8080/day10/3.jsp?username=ascv&password=1234567&marry=true -->
<br/>
${paramValues.username[0]}——${paramValues.username[1] }——${paramValues.password[0] }
<!-- 请求方式:http://localhost:8080/day10/3.jsp?username=ascv&password=1234567&username=ergg&marry=true -->
<h2>header对象的使用</h2>
${header["accept-encoding"] }<br/>
${headerValues["accept-encoding"][0] }
<h2>Cookie对象的用法</h2>
${cookie }
<br>
${cookie["JSESSIONID"].name }<br><!-- 得到cookie对象本身名称 -->
${cookie.JSESSIONID.value }
<h2>initParam对象</h2>
${initParam.Encodingvalue }<!-- 从xml中得到全局初始化参数值 ,命中和值是任意的-->
<!-- xml中加入
<context-param>
<param-name>Encodingvalue</param-name>
<param-value>UTF-99</param-value>
</context-param>
-->
</body>
</html>
结果: