首页 > 设计 > WEB开发 > 正文

11个EL隐式对象

2019-11-02 18:31:50
字体:
来源:转载
供稿:网友

1、与作用域范围有关的四个隐式对象

pageScope、requestScope、sessionScope和applicationScope。​

它们可以读取使用jsp内置对象pageContext、request、session以及application的setAttribute()方法所设定的对象的数值-----即getAttribute(Stringname),却不能取得其他相关信息。​

​  例如,要取得session中储存的一个username属性的值,可以利用下面的方法:session.getAttribute("username")

在EL中则使用下面的方法:${sessionScope.username}

注意:如果没有设定使用EL内置对象的作用范围,则按照pageScope、requestScope、sessionScope和applicationScope的先后顺序读取属性值。

即,通知JSP引擎调用pageContext.findAttribute()方法,以标识符为关键字从各个域对象中获取对象。如果域对象中不存在标识符所对应的对象,则返回结果为“”(注意,不是null)。

2、​与输入有关的两个隐式对象

param和​paramValue。主要用于取得请求参数。

​ request.getParameter(Stringname)

request.getParameterValues(String name)

在EL中则可以使用param和paramValues两者来取得数据:

${param.name}*****${paramValues.name}

3、cookie

用来取得使用者的cookie值,例如在cookie中设定了username属性值,可以使用${cookie.username.value}​

4、header和headerValues

​读取请求的头数据,使用header或headerValues内置对象,例如${header[“User-Agent”]},headerValues则用来取得所有的头信息,等价于调用request.getHeaders()方法。

5、initParam

​initParam用来读取设置在web.xml中的参数值。例如${initParam.repeat},

等价于:(String)application.getInitParameter(“repeat”); 

或:servletContext.getInitParameter(“repeat”);

6、pageContext

pageContext用于取得其他有关用户要求或页面的详细信息

​${pageContext.request.queryString}取得请求的参数字符串

${pageContext.request.requestURL} 取得请求的URL,不包括参数字符串

${pageContext.request.contextPath}        服务的web application 的名称

${pageContext.request.method}          取得HTTP 的方法(GET、POST)

${pageContext.request.PRotocol}        取得使用的协议(HTTP/1.1、HTTP/1.0)

${pageContext.request.remoteUser}        取得用户名称

${pageContext.request.remoteAddr}        取得用户的ip 地址

${pageContext.session.new}            判断session 是否为新的

${pageContext.session.id}              取得session 的ID

${pageContext.servletContext.serverInfo}  取得主机端的服务信息

查看JSP九个隐式对象点击打开链接(http://blog.csdn.net/j080624/article/details/52965366)


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表