属性集合对象

  1. session 对象
  2. application 对象
  3. pageContent 对象
  4. request 对象
对象实例名 描述
application 全局范围
pageContent 本页范围
session 当前用户范围
request 页面传递范围
属性/方法 描述
void setAttribute(String name, Object value) 设定 name 属性的值为 value
Enumeration getAttributeNamesInScope(int scope ) 取得所有 scope 范围的属性,不支持 pageContent
Object getAttribute(String name) 取得 name 属性的值
void removeAttribute(String name) 移除 name 属性的值
   
   
getParameter("name") 获取传递的参数

示例:

String Name = (String)pageContext.getAttribute("Name");
Integer Year = (Integer)session.getAttribute("Year");
pageContext.setAttribute("Year", new Integer(2001));
request.setAttribute("Month", new Integer(12) );
session.setAttribute("Day", new Integer(27) );
application.setAttribute("Times", new Integer(10));

session 对象

属性/方法 描述
long getCreationTime() 取得session产生的时间,单位是毫秒,由1970年1 月1 日零时算起
String getId() 取得session 的ID
long getLastAccessedTime() 取得用户最后通过这个session送出请求的时间,单位是毫秒,由1970 年1 月1 日零时算起
long getMaxInactiveInterval() 取得最大session不活动的时间,若超过这时间,session 将会失效,时间单位为秒
void invalidate() 取消session 对象,并将对象存放的内容完全抛弃
boolean isNew() 判断session 是否为"新"的,所谓"新"的session,表示session 已由服务器产生,但是client 尚未使用
void setMaxInactiveInterval(int interval) 设定最大session不活动的时间,若超过这时间,session 将会失效,时间单位为秒

application 对象

属性/方法 描述
int getMajorVersion( ) 取得Container 主要的Servlet API 版本,如:2
int getMinorVersion( ) 取得Container 次要的Servlet API 版本,如:4
String getServerInfo( ) 取得Container 的名称和版本
有关服务端的路径和文件的方法 描述
String getMimeType(String file) 取得指定文件的MIME 类型
ServletContext getContext(String uripath) 取得指定 Local URL 的Application
context String getRealPath(String path) 取得本地端 path 的绝对路径
有关信息记录的方法 描述
void log(String message) 将信息写入log 文件中
void log(String message, Throwable throwable) 将stack trace 所产生的异常信息写入 log文件中

pageContext 对象

取得其他隐含对象的方法 描述
Exception getException( ) 回传目前网页的异常,不过此网页要为error page,例如:exception 隐含对象
JspWriter getOut( ) 回传目前网页的输出流,例如:out 隐含对象
Object getPage( ) 回传目前网页的Servlet 实体(instance),例如:page 隐含对象
ServletRequest getRequest( ) 回传目前网页的请求,例如:request 隐含对象
ServletResponse getResponse( ) 回传目前网页的响应,例如:response 隐含对象
ServletConfig getServletConfig( ) 回传目前此网页的ServletConfig 对象,例如:config 隐含对象
ServletContext getServletContext( ) 回传目前此网页的执行环境(context),例如:application隐含对象
HttpSession getSession( ) 回传和目前网页有联系的会话(session),例如:session 隐含对象取得属性的方法
取得属性方法 描述

Object getAttribute(String name, int scope)

回传name 属性,范围为scope 的属性对象, 回传类型为 java.lang.Object

Enumeration getAttributeNamesInScope(int scope)

回传所有属性范围为scope 的属性名称,回传类型为Enumeration

int getAttributesScope(String name)

回传属性名称为 name 的属性范围,仅页面范围

void removeAttribute(String name)

移除属性名称为name 的属性对象

void removeAttribute(String name, int scope)

移除属性名称为name,范围为scope 的属性对象

void setAttribute(String name, Object value, int scope)

指定属性对象的名称为name、值为value、范围为scope

Object findAttribute(String name)

寻找在所有范围中属性名称为 name 的属性对象,查找范围依次如下:

page,request,session,application

所提供范围的变量 描述

PAGE_SCOPE

存入pageContext 对象的属性范围

REQUEST_SCOPE

存入request 对象的属性范围

SESSION_SCOPE

存入session 对象的属性范围

APPLICATION_SCOPE

存入application 对象的属性范围

exception 对象