JSP中的EL隐式对象

简介:

一 介绍 

      JSP表达式语言(Expression Language),简称EL,为存取变量、表达式运算和读取内置对象等内容提供了新的操作方式。目的:为了使JSP写起来更加简单。表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方法。

${ param. username } 等价于 <%=request. getParameter(“username”) %>

${ requestScope.userlist } 等价于 <%=request.getAttribute(“userlist”) %>

同样,${ sessionScope.user.name } 等价于 ${ sessionScope.user.[“name”] } 或者 ${ sessionScope.users[0].name }

 EL隐式对

EL中属性的范围

属性范围 EL中的名称
Page     PageScope
Request RequestScope
Session SessionScope
Application ApplicationScope

EL表达式还有一些隐式的对象,一共11个,具体如下:

EL中隐含的对象

隐含对象 说明
pageScope 取得Page范围属性名称中的值
requestScope 取得Request范围属性名称中的值
sessionScope 取得Session范围属性名称中的值
applicationScope 取得Application范围属性名称中的值
pageContext 表示JSP中的PageContext
param 同:ServletRequest.getParameter(String name)
paramValues 同:ServletRequest.getParameterValues(String name)
header 同:ServletRequest.getHeader(String name)
headervalues 同:ServletRequest.getHeaders(String name)
cookie 同:ServletRequest.getCookies( )
initparam 同:ServletRequest.getInitParameter(String name)

三 关于EL隐式对象的简单实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
< html >
< head >
< title >EL表达式简单实例</ title >
</ head >
 
< body >
     < form  action = "elshow.jsp"  method = "post" >
         < table  width = "752"  height = "367"  border = "1" >
             < tr >
                 < td  width = "71" >姓名:</ td >
                 < td  width = "252" >< label  for = "textfield" ></ label > < input
                     type = "text"  name = "name"  id = "textfield"  /></ td >
             </ tr >
             < tr >
                 < td >性别:</ td >
                 < td >< input  type = "radio"  name = "sex"  id = "radio"  value = "男"  /> < label
                     for = "radio" > 男 < input  type = "radio"  name = "sex"  id = "radio2"
                         value = "女"  /> 女
                 </ label ></ td >
             </ tr >
             < tr >
                 < td >年龄:</ td >
                 < td >< label  for = "textfield2" ></ label > < input  name = "age"
                     type = "text"  id = "textfield2"  size = "5"  /></ td >
             </ tr >
             < tr >
                 < td >爱好:</ td >
                 < td >< table  width = "526"  height = "90"  border = "0" >
                         < tr >
                             < td  width = "116" >< label > < input  type = "checkbox"
                                     name = "interest"  value = " 打篮球"  id = "interest_0"  /> 打篮球
                             </ label > < br  /></ td >
                             < td  width = "105" >< input  type = "checkbox"  name = "interest"
                                 value = "网上冲浪"  id = "interest_1"  /> 网上冲浪</ td >
                             < td  width = "68" >< input  type = "checkbox"  name = "interest"
                                 value = "旅游"  id = "interest_2"  /> 旅游</ td >
                             < td  width = "66" >< input  type = "checkbox"  name = "interest"
                                 value = "健身"  id = "interest_3"  /> 健身</ td >
                         </ tr >
                     </ table >
                     < p >
                         < br  />
                     </ p ></ td >
             </ tr >
             < tr >
                 < td  colspan = "2" >< input  type = "submit"  name = "button"  id = "button"
                     value = "提交"  /></ td >
             </ tr >
         </ table >
     </ form >
</ body >
</ html >

elshow.jsp页面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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" >
     < base  href="<%=basePath%>">
     < title >Insert title here</ title >
</ head >
< body >
     <%
         request.setCharacterEncoding("utf-8");
         request.setAttribute("r", "requsetTest");
         session.setAttribute("s", "sessionTest");
         application.setAttribute("a", "applicationTest");
         pageContext.setAttribute("p", "pageContextTest");
     %>
     姓名:${param.name}< br >
     性别:${param.sex}< br >
     年龄:${param.age}< br >
     爱好:
         < c:forEach  items = "${paramValues.interest}"  var = "interest" >
         ${interest}    
         </ c:forEach >< br >
         < hr />
         request示例:< br />
         ${requestScope.r}< br />   < hr />  
         session示例:< br />
         ${sessionScope.s}< br />   < hr />  
         application示例:< br />
         ${applicationScope.a}< br /> < hr />    
         pageContext示例:< br />
         ${pageScope.p}< br /> 
         
         <!-- pageContext用于取得其他有关用户要求或页面的详细信息 -->
         ${pageContext.request.queryString} < br  />< hr  />  <!-- 取得请求的参数字符串 -->
         ${pageContext.request.requestURL} < br  />< hr  />  <!-- 取得请求的URL,不包括参数字符串 -->
         ${pageContext.request.contextPath} < br  />< hr  />  <!-- web application的名称 -->
         ${pageContext.request.method} < br  />< hr  />  <!-- 取得提交的HTTP方法(PS:GET或POST) -->
         ${pageContext.request.protocol} < br  />< hr  />  <!-- 取得使用的协议 -->
         ${pageContext.request.remoteUser} < br  />< hr  />  <!-- 取得客户端的名称 -->
         ${pageContext.request.remoteAddr} < br  />< hr  />  <!-- 取得客户端的IP地址 -->
         ${pageContext.session.id} < br  />< hr  />  <!-- 取得session 的ID -->
         ${pageContext.servletContext.serverInfo} < br  />< hr  />  <!-- 取得客户端的服务信息 -->
</ body >
</ html >

测试效果:

wKioL1a_Vu3xG5nBAAEQ92lBSgk855.png

wKioL1a_Vu3wct-kAAEVwDqMH44603.png



本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1741856,如需转载请自行联系原作者


相关文章
|
5月前
|
缓存 Java 开发者
JSP 教程 之 JSP 隐式对象 10
JSP隐式对象是预定义的Java对象,如request、response、out、session、application等,直接供开发者使用。out对象是JspWriter实例,用于向网页输出内容,类似PrintWriter但有缓存处理和抛出IOException。exception对象保存了页面的异常信息,用于错误处理。
32 3
|
5月前
|
Java 开发者 容器
JSP 教程 之 JSP 隐式对象 3
JSP教程讲解了九个隐式对象,如request、response、out、session、application、config、pageContext、page和Exception。response对象是HttpServletResponse的实例,用于向客户端发送响应,包括设置HTTP头信息、添加cookie和状态码。
30 3
|
5月前
|
Java 开发者 容器
JSP 教程 之 JSP 隐式对象 2
JSP隐式对象是预定义的Java对象,无需声明即可直接使用。包括9大对象:request(HttpServletRequest,处理客户端请求)、response(HttpServletResponse,响应输出)、out(JspWriter,输出到网页)、session(HttpSession,用户会话管理)、application(ServletContext,应用上下文)、config(ServletConfig,配置信息)、pageContext(PageContext,全局访问)、page(当前页面对象)和Exception(异常对象)。
33 3
|
5月前
|
缓存 Java 开发者
JSP 教程 之 JSP 隐式对象 5
JSP教程介绍了九个隐式对象,如request、response、out、session、application等,无需声明即可直接使用。out对象是JspWriter实例,用于向网页输出内容,可配置是否缓存。它扩展了PrintWriter功能,处理缓存并可能抛出IOException。session对象基于HttpSession,用于跨客户端请求的会话跟踪。
26 1
|
5月前
|
Java 开发者 容器
JSP 教程 之 JSP 隐式对象 1
**JSP隐式对象**是无需声明的预定义Java对象,如`request`(HttpServletRequest)处理请求,`response`(HttpServletResponse)构造响应,`out`(JspWriter)输出到页面,`session`(HttpSession)管理会话,`application`(ServletContext)涵盖整个应用,`config`(ServletConfig)提供配置信息,`pageContext`(PageContext)访问页面上下文,`page`类似Java的`this`,及`Exception`对象捕获页面异常。
22 2
|
5月前
|
缓存 Java 开发者
JSP 教程 之 JSP 隐式对象 9
JSP隐式对象是预定义的Java对象,无需声明即可直接使用。包括9大对象:request(HttpServletRequest)、response(HttpServletResponse)、out(JspWriter,用于输出到网页)、session(HttpSession)、application(ServletContext)、config(ServletConfig)、pageContext(PageContext,访问所有对象和命名空间)、page(页面实例,等同于Java的this)和Exception(异常对象)。
35 0
|
5月前
|
缓存 Java 对象存储
JSP 教程 之 JSP 隐式对象 8
JSP隐式对象是预定义的Java对象,如request、response、out等,无需声明即可直接使用。out对象是JspWriter实例,用于向response输出内容,支持print和println方法,可抛出IOException。pageContext对象代表整个JSP页面,提供访问页面信息和不同scope的对象,如request、response、session等,并有removeAttribute()方法管理页面属性。
30 0
|
5月前
|
缓存 Java 开发者
JSP 教程 之 JSP 隐式对象 7
JSP隐式对象是预定义的Java对象,无需声明即可使用。包括9大对象:request、response、out、session、application、config、pageContext、page和Exception。其中,out对象用于输出内容到网页,基于JspWriter,支持print和println方法。config对象是ServletConfig的实例,提供访问Servlet初始化参数的途径,如获取servlet名称。
27 0
|
5月前
|
缓存 Java 开发者
JSP 教程 之 JSP 隐式对象 6
JSP隐式对象是预定义的Java对象,无需声明即可直接使用。包括:request(HttpServletRequest)、response(HttpServletResponse)、out(JspWriter,用于输出到网页)、session(HttpSession)、application(ServletContext)、config(ServletConfig)、pageContext(PageContext)、page(当前页面对象)和Exception。out对象基于JspWriter,用于内容输出,提供print和println方法。
26 0
|
5月前
|
缓存 Java 开发者
JSP 教程 之 JSP 隐式对象 4
JSP隐式对象是预定义的Java对象,无需声明即可直接使用。包括9大对象:request、response、out、session、application、config、pageContext、page和Exception。其中,out对象基于JspWriter,用于向response写入内容,具有缓存管理功能,并扩展了PrintWriter的方法。常用方法有print()、println()用于输出各种类型数据,以及flush()刷新输出流。
29 0