javaweb之jsp内置对象

来源:互联网 发布:云软件多少钱呢 编辑:程序博客网 时间:2024/05/21 06:23

JSP内置对象

什么叫内置对象:JSP翻译为Servlet后,在代码中已经存在对象,JSP页面可以直接使用这些对象

l request HttpServletRequest

l response HttpServletResponse

l session HttpSession

l application            ServletContext

l config ServletConfig

l page this (HttpServlet)

l pageContext PageContext

l exception            Throwable (所有异常父类)

l out            JspWriter      

 

service方法参数内: requestresponse

方法内部定义6

    PageContext pageContext = null;

    HttpSession session = null;

    ServletContext application = null;

    ServletConfig config = null;

    JspWriter out = null;

    Object page = this;

如果jsp设置 isErrorPage=true

    Throwable exception = org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable(request);

    if (exception != null) {

      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

    }

 

Servlet三种数据范围 : requestsessionServletContext

JSP存在四种数据范围 : page request session application

* application 就是 ServletContext

* page代表当前页面,保存数据到当前页面数据范围, 该数据只能在当前页面使用

 

Ø Page对象

 

“page” 对象代表了正在运行的由JSP文件产生的类对象 【一般不建议使用】

 

page对象是指向当前JSP程序本身的对象this

 

page对象其实是java.lang.Object类的实例对象

 

Ø pageContext对象

 

pageContext代表当前页面上下文

1、常用操作一 :向 JSP四个范围 存取数据

setAttribute(String name, Object value, int scope)

getAttribute(String name, int scope)

 

* setAttribute(String name, Object value)  没有指定scope,默认将数据保存到page范围

 

提供了 非常重要方法 findAttribute(String name)  : 按照page - request - session - application搜索 指定属性,找到了返回,如果都没有返回 null

 

2、常用操作二:获取其它八个隐含对象

* 意义:编写框架,获得pageContext一个对象,可以获得JSP其它八个内置对象

getException方法返回exception隐式对象

getPage方法返回page隐式对象

getRequest方法返回request隐式对象

getResponse方法返回response隐式对象

getServletConfig方法返回config隐式对象

getServletContext方法返回application隐式对象

getSession方法返回session隐式对象

getOut方法返回out隐式对象

 

Ø out对象

Ø 向客户端输出数据

Ø 管理服务器输出缓冲区

Ø 内部使用PrintWriter对象来输出文本级数据

Ø 通过page指令的buffer属性来调整缓冲区的大小,默认的缓冲区是8kb

 

Ø Exception

 

exception对象是java.lang.Trowable类的实例

(使用前 isErrorPage=“true”)

exception对象用来处理JSP文件在执行时所有发生的错误和异常

 

exception对象可以和page指令一起使用,通过指定某一个页面为错误处理页面,对错误进行处理

• <%@ page isErrorPage=”true “%>的页面内使用

1 0