欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP详细篇——EL表达式(二)

程序员文章站 2022-06-24 23:18:42
el的隐含对象 为了能够获得web应用中的是相关数据,el提供了11个隐含对象,这些对象类似于jsp的内置对象,也是直接通过对象名进行操作。在el的隐含对象中,除了pagecontex...

el的隐含对象

为了能够获得web应用中的是相关数据,el提供了11个隐含对象,这些对象类似于jsp的内置对象,也是直接通过对象名进行操作。在el的隐含对象中,除了pagecontext是javabean对象,对应于javax.servlet..pagecontext类型外,其他的隐含对象都对应于java.util.map类型。这些隐含对象可以分为页面上下文对象、访问作用域范围的隐含对象和访问环境信息的隐含对象3种。下面分别进行详细介绍。

1、页面上下文对象

页面上下文对象为pagecontext,用于访问jsp内置对象(request/response/out/session/exception和page,但是不能用于获取application/config/pagecontext)和servletcontext。在获取到这些内置对象后,就可以获取其属性。这些属性与对象的getxxx()方法相对应,在使用时,去掉方法名中的get,并将首字母改为小写即可。

访问request对象

通过pagecontext获取jsp内置对象中的request对象,可以使用以下语句:

${pagecontext.request}

获取到request对象后,就可以通过该对象获取与客户端相关的信息。例如:http报头信息、客户信息提交方式、客户端ip地址和端口号等。

范例:

要访问getserverport()方法,可以使用以下的代码:

${pagecontext.request.serverport}

以上代码将返回端口号。

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

my jsp 'request.jsp' starting page

${pagecontext.request.servername }

${pagecontext.request.serverport }

${pagecontext.request.servletpath }

访问response对象

通过pagecontext获取jsp内置对象中的response对象,可以使用下面的语句:

${pagecontext.response}

范例:

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

my jsp 'response.jsp' starting page

${pagecontext.response.buffersize }

${pagecontext.response.characterencoding }

访问out对象

通过pagecontext访问out对象,使用以下语法:

${pagecontext.out}

范例:

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

my jsp 'out.jsp' starting page

${pagecontext.out.buffersize }

${pagecontext.out.remaining}

访问session对象

访问session对象的语法格式为:

${pagecontext.session}

范例:

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

my jsp 'session.jsp' starting page

${pagecontext.session.id }

访问exception对象

通过pagecontext获取jsp内置对象的exception对象的语法个格式为:

${pagecontext.exception}

范例:

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

my jsp 'exception.jsp' starting page

${pagecontext.exception.localizedmessage }

访问page对象

通过pagecontext访问page对象的语法格式为:

${pagecontext.page}

范例:

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

my jsp 'page.jsp' starting page

${pagecontext.page.class }

访问servletcontext对象

语法格式如下:

${pagecontext.servletcomtext}

范例:

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

my jsp 'servletcontext.jsp' starting page

${pagecontext.servletcontext.contextpath }

2、访问作用域范围的隐含对象

在el中提供了4种用于访问作用域范围的隐含对象,即pagescope/requestscope/sessionscope/applicationscope。应用这4个隐含对象指定所要查找的标识符的作用域后,将不再按照默认的顺序(page/request/session/application)来查找相应的标识符。他们与jsp中的page/request/session/application内置对象相似。不过,这4个隐含对象只能用于取得指定范围内的属性,而不能取得其他相关信息。

pagescope隐含对象

pagescope隐含对象用于返回包含page范围的属性值的集合,返回值为java.util.map对象。

范例:

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

my jsp 'pagescope.jsp' starting page

${pagescope.user.name }

requestscope隐含对象

requestscope隐含对象用于返回包含request范围内的属性的集合。返回值为java.util.map对象。

范例:

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

my jsp 'requestscope.jsp' starting page

${requestscope.user.name }

sesssionscope隐含对象

sessionscope隐含对象用于返回session范围内的属性值的集合。返回值为java.util.map对象。

范例:

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

my jsp 'sessionscope.jsp' starting page

${sessionscope.user.name }

applicationscope隐含对象

applicationscope隐含对象用于返回包含application范围的属性值的集合,返回值为java.util.map对象

范例:

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

my jsp 'applicationscope.jsp' starting page

javascript"/>

${applicationscope.user.name }

3、访问环境信息的隐含对象

在el中提供了6个访问环境信息的隐含对象。

a、param 对象

param 对象用于获取请求参数的值,应用在参数值只有一个的情况。在应用param对象时,返回的结果为字符串。

范例:

在jsp页面中,放置一个名为user的文本框:

当表单提交后,要获取name文本框的值,使用如下方式:

${param.name}

ps:

在name文本框中如果输入了中文,那么在使用el输出其内容的时候,要使用request.setcharacterencoding(“gbk”);语句设置请求的编码为支持中文的编码,不然将出现乱码。

b、paramvalues对象

如果一个请求参数名对应多个值,则需要使用paramvalues对象获取请求的参数值。在应用paramvalues对象时,返回的结果是数组。

范例:

在jsp页面中,放置一个名称为affect的复选框。

登山

游泳

慢走

晨跑

当提交表单后,要获取affect的值,可以使用下面的形式:

爱好为:${paramvalues.affect[0]}${paramvalues.affect[1]}${paramvalues.affect[2]}${paramvalues.affect[3]}

在使用param和paramvalues对象时,如果指定的参数不存在,则返回空字符串而不是返回null

c、header和headervalues对象

header对象用于获取http请求的一个具体的header的值,但是在有些情况下,可能窜在同一个header拥有多个不同的值的情况,这就要使用到headervalues对象

范例:

要获取http请求的header的connection属性,可以使用如下形式:

${header.connection}或者${header[“connection]}

但是,如果要获取http请求的header的user-agent属性,则必须使用以下el表达式:

${header[“user-agent”]}

d、initparam对象

initparam对象用于获取web应用的初始化参数的值

范例:

在web应用的web.xml中设置一个初始化参数author,用于指定作者。

author

mr

运用el表达式获取参数author:

${initparam.author}

f、cookie对象

在el中没有提供向cookie中保存值的方法,但是提供了访问由请求设置的cookie方法,这可以通过cookie隐含对象来实现。如果在cookie中已经设定好了一个名称为username的值,那么可以通过${cookie.username}来获取该cookie对象。如果要获取cookie中的值,需要使用cookie对象的value属性

范例:

使用response对象设置一个请求有效的cookie对象,然后在使用el获取该cookie对象的值,可以使用以下代码:

cookie cookie=new cookie(“user”,”zhangsan”);

response.addcookie(cookie);

%>

${cookie.user.value}

JSP详细篇——EL表达式(二)