JSP 隐含对象
数据类型则要进行对象数据类型的转换。如整数1,则转换为 new Integer(1); •获得属性 – 当我们使用getAttribute(String name)取得name 属性的值时, 它返回一个ng.Object类型的对象,因此,还必须根据 name 属性值的类型进行类型转换 (Casting)工作。 – 例如,如果要取得String 类型的Name 属性时: String userName = (String)pageContext.getAttribute(“userName”); 或者String userName = pageContext.getAttribute(“userName”).toString();
• 3 、JSP中的隐含对象
• 4、错误处理
3 Sept. 2008 Confidential
JSP中隐含对象的概念
一. 什么是JSP的隐藏对象 二. JSP中有哪些隐藏对象
3 Sept. 2008 Confidential
JSP隐藏对象的由来
下面是一个普通的jsp页面文件helloworld.jsp
对当前JSP页面的引用,即 Java中的this
JSP页面的ServletConfig对象 响应信息 JSP的数据输出对象 异常处理
3 Sept. 2008 Confidential
主要内容
• 1 、JSP中隐含对象的定义 • 2 、属性(Attribute) 与范围
• 3 、JSP中的隐含对象
• 4、错误处理
Object getAttribute(String name)
void removeAttribute(String name)
取得name属性的值
删除名称为name的属性
3 Sept. 2008 Confidential
使用属性的一般注意事项1
•设置属性 – setAttribute(属性名,属性值)方法用来设置属性。 – 其中属性名为字符串类型。属性值为Object类型,如果是原子
3 Sept. 2008 Confidential
属性与范围
一.哪些JSP隐藏对象可以保存属性 二.属性的作用范围(Scope)
3 Sept. 2008 Confidential
可以使用属性的隐藏对象
我们通常会将request、session、application 和pageContext 归为一类, 原因在于:它们皆能借助属性来做到数据分享。 下表 列出了一般储存和取得属性的方法,这些方法pageContext、request、 session和application皆可使用。 方法 void setAttribute(String name, Object value) Enumeration getAttributeNamesInScope(int scope ) 说明 设定名称为name的属性, 属性的设置为value 取得所有scope范围的属 性名称组成的列举表
属性的范围(Scope)---page
概念 属性的范围为page,指的是属性只作用在当前的JSP 页面范围中。 操作方法 – 如果要将数据存入Page 范围,可用 pageContext 对象的setAttribute( )方法; – 若要取得page范围的数据,可用pageContext对 象的getAttribute( )方法。
javax.servlet.HttpSession
javax.servlet.ServletContext
表示会话对象
JSP页面所在Web应用的上下 文对象
page
config response out exception
ng.Object
javax.servlet.ServletConfig javax.servlet.HttpServletResponse javax.servlet.JspWriter ng.Throwable
–
如果是Integer 类型的Year 属性时: Integer Year = (Integer)session.getAttribute("Year");
3 Sept. 2008 Confidential
使用属性的一般注意事项2
•
在使用属性存储数据前,应该首先根据对属性的功能 要求,确定准备将属性存储在Request、Session 、 Application或PageContext的哪个之中,以便将相应 数据存入选定对象之中。 存入不同隐藏对象的属性在功能上有很大区别。
3 Sept. 2008 Confidential
public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; ... _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType("text/html;charset=GBK"); 我们察看jsp页面转译后的_jspService方法 pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true); 时发现,在该方法内,jsp转译引擎生成 application = pageContext.getServletContext(); 了一些局部对象并对它们进行了初始化。 config = pageContext.getServletConfig(); 这就意味着,在jsp的脚本元素中,我们 session = pageContext.getSession(); 可以直接使用这些局部对象(还有request、 out = pageContext.getOut(); response两个参数对象)而不必声明它们。 ...
}
3 Sept. 2008 Confidential
JSP隐藏对象的概念
概念 jsp页面在运行之前的转译过程中,jsp引擎为其_jspService方法 定义了一些局部对象。由于jsp脚本都将转译成_jspService方法 的代码,因此这些局部对象在jsp脚本中是可直接使用的。这些 局部对象一个通常的叫法为隐藏对象。 通过分析_jspService方法的代码,可知常用的隐藏对象有: – pageContext PageContext 类型 – session HttpSession 类型 – application ServletContext 类型 – config ServletConfig 类型 – out JspWriter 类型 – page Object 类型 – request HttpServletRequest类型 – response HttpServletResponse 类型
这一点十分重要,这是因为这将决定属性的作用范围。换句话 说,属性的宿主对象决定了属性的作用域、生存期等重要特性。
范围 JSP的属性有四种宿主对象,也就有了四种作用范围 – page – request – session – application。
3 Sept. 2008 Confidential
<%@ page contentType="text/html;charset=GB2312" %> <html> <head> <title>CH5 - helloworld.jsp</title> </head> <body> <% out.println("Helloworld,JSP!"); %> </body> </html> 转译 我们知道,该文件被第一次浏览时,jsp引擎将对其进行转译。
Java Web技术
3 Sept. 20072008 By Neusoft Group. All rights reserved Copyright © Neusoft Confidential
JSP隐含对象
3 Sept. 2007 © Neusoft Confidential
主要内容
• 1 、JSP中隐含对象的定义 • 2 、属性(Attribute) 与范围
3 Sept. 2008 Confidential
属性的范围(Scope)---page
例子: ------------PageScope1.jsp ----------------<%@ page contentType="text/html;charset=GB2312" %> html> <head> <title>CH6 - PageScope1.jsp</title> 我们在PageScope1.jsp页面中加入两个 </head> page范围的属性:Name和Passwrd: <body> <h2>Page 范围 - pageContext</h2> <% pageContext.setAttribute("Name","scott"); pageContext.setAttribute("Password","tiger"); %> <jsp:forward page="PageScope2.jsp"/> </body> PageScopes.jsp将被调用。问题是,上 </html> 述两个属性在PageScope2.jsp中是否仍