JSP内置对象

来源:互联网 发布:hbase java 条件查询 编辑:程序博客网 时间:2024/06/05 01:36
1、JSP有哪些内置对象,作用分别是什么?
  答:
(1)out对象:是javax.servlet.jsp.JspWriter类的实例。服务器向客户端输出的字符类内容可以通过out对象输出。
               方法名                          描述
  void append(CharSequence cs)     向out缓存中扩展字符类输出。当缓存满或者执行 
                                   out.flush()操作时,这些内容会输出到客户端浏览器。
  void clear()                     清空要输出的内容
  void flush()                     将缓存内容flush到客户端浏览器
  void println(String str)         向客户端输出内容
  boolean isAutoFlush()            返回缓存满时是否自动flush,如果为false,抛出异常
  int getBufferSize()              返回缓存大小,单位KB
  int getRemaining()               返回缓存剩余大小,单位KB

(2)request对象:是javax.servlet.ServletRequest类的实例。代表着客户端的请求。
          方法名                                        描述
 void setAttribute(String name,Object value)        
 Object getAttribute(String name)
 String getMethod()
 String getParameter(String key)
 String[] getParameterValues(String key)
 Enumeration  getAttributeNames(String name)
 Cookie[] getCookies()
 String getContextPath()
 String getRequestURI()
 void setCharacterEncoding(String encoding)
 String getHeader(String name)
 Enumeration getHeaderNames()
 Dispather getRequestDispather()
 HttpSession getSession()

(3)response响应对象:是javax.servlet.ServletResponse类的实例,代表着客户端的响应。
        方法名                          描述
 void clear()                   清空暂存在缓冲区的输出
 void addCookie(Cookie cookeie) 设置Cookie
 OutputStream getOutputStream() 返回服务器输出流。
 void sendRedirect(String url)  使本页面redirect到另一个页面
 void setContentType(String contentType) 设置文档类型。HTML是text/html
 PrintWriter getOut()           返回out对象
 void setHeader()               设置response头信息
 void serStatus(int status)     设置response状态码
(4)config配置对象:javax.servlet.ServletConfig类的实例。ServletConfig封装了配置在web.xml中初始化JSP的参数。JSP中获取这些参数。

          方法名                          描述
 String getInitParameter(String name)  返回配置在web.xml中的初始化参数
 Enumeration getInitPatameterNames()   返回所有的初始化参数名称
 ServletContext getServletContext()    返回ServletContext对象
 String getServletName()               返回Servlet的名称

(5)session会话对象:是javax.servlet.http.HttpSession类的实例。session和cookie是记录客户访问信息的两种机制,session用于在服务器端保存用户信息,cookie用于在客户端保存用户信息。

        方法名                                       描述
String getId()                                  返回session的id
Object getAttribute(String name)                返回session中属性名为name的对象
Enumeration  getAttributeNames(String name)     返回session的所有属性名
long getCreationTime()                          返回该session创建的时间
long getLastAccessedTime()                      返回该session最后一次访问的时间
int getMaxInactiveInterval()                    返回session的最大允许的间隔时间。单位为秒
void setAttribute(String name,Objext value)     设置session
void setMaxInactiveInterval(long second)        设置最大允许的时间间隔

(6)application应用程序对象:javax.servlet.ServletContext类的对象。application封装了JSP所在的Web应用程序的信息。Servlet中application对象需要通过ServletConfig.getServletContext()来获取。整个Web应用程序对应一个application对象。

      方法名                                           描述
Object getAttribute(String name)                返回application中属性名为name的对象
Enumeration  getAttributeNames(String name)     返回application中所有的属性名
void setAttribute(String name ,Object value)    设置application的属性
void removeAttribute(String name )              移除application的属性
String getInitParameter(String name)            返回全局初始化参数
Enumeration getInitParameterNames()             返回所有的全局初始化参数
String getMimeTyep(String filename)             返回文件的文档类型
String getRealPath(String relativePath)         返回web应用程序内相对网址对应的绝对路径

(7)page页面对象:是javax.servlet.jsp.HttpJspPage类的实例。page对象代表当前JSP页面,是当前JSP编译后的Servlet类的对象。page相当于普通java类的关键字this。
     
(8)pageContext页面上下文对象:pageContext为javax.servlet.jsp.PageContext类的例。PageContext对象代表当前JSP页面编译后的内容。通过PageContext能够获取到JSP中的资源

       方法名                                描述
Object findAttribute(String name)            在JSP页面中查找变量
void forward(String url)                     跳转到另一个页面
Object getAttribute(String name)             返回属性
Object getAttribute(String name,int scope)   返回指定范围内的属性。范围包括
                                             PAGE_SCOPE,REQUEST_SCOPE,SESSION_REQUEST,APPLICATION_SCOPE
JspWriter getOut()                           返回out对象
Object getPage()                             返回page对象
ServletRequest getRequest()                  返回request对象
ServletResponse getResponse()                返回response对象
HttpSession getSession()                     返回session对象

(9)exception对象:exception是javax.servlet.Exception类的对象。exception封装了JSP中抛出的异常信息。要使用exception隐藏对象,需要设置<%@page isErrorPage="true"%>.隐藏对象exception通常被用来处理错误页面。













0 0
原创粉丝点击