JSP基础--九大内置对象

≯℡__Kan透↙ 提交于 2019-11-26 16:48:28

JSP九大内置对象

 

1 什么是JSP九大内置对象

在JSP中无需创建就可以使用的9个对象,它们是:

l  out(JspWriter):等同与response.getWriter(),用来向客户端发送文本数据;

l  config(ServletConfig):对应“真身”中的ServletConfig;

l  page(当前JSP的真身类型):当前JSP页面的“this”,即当前对象;

l  pageContext(PageContext):页面上下文对象,它是最后一个没讲的域对象;

l  exception(Throwable):只有在错误页面中可以使用这个对象;

l  request(HttpServletRequest):即HttpServletRequest类的对象;

l  response(HttpServletResponse):即HttpServletResponse类的对象;

l  application(ServletContext):即ServletContext类的对象;

l  session(HttpSession):即HttpSession类的对象,不是每个JSP页面中都可以使用,如果在某个JSP页面中设置<%@page session=”false”%>,说明这个页面不能使用session。

 

在这9个对象中有很多是极少会被使用的,例如:config、page、exception基本不会使用。

在这9个对象中有两个对象不是每个JSP页面都可以使用的:exception、session。

在这9个对象中有很多前面已经学过的对象:out、request、response、application、session、config。

 

2 通过“真身”来对照JSP

 

  我们知道JSP页面的内容出现在“真身”的_jspService()方法中,而在_jspService()方法开头部分已经创建了9大内置对象。

 

  public void _jspService(HttpServletRequest request, HttpServletResponse response)[崔1] 

        throws java.io.IOException, ServletException {

 

    PageContext pageContext = null;[崔2] 

    HttpSession session = null;[崔3] 

    ServletContext application = null;[崔4] 

    ServletConfig config = null;[崔5] 

    JspWriter out = null;[崔6] 

    Object page = this;[崔7] 

    JspWriter _jspx_out = null;

    PageContext _jspx_page_context = null;

 

 

    try {

      response.setContentType("text/html;charset=UTF-8");

      pageContext = _jspxFactory.getPageContext(this, request, response,

                            null, true, 8192, true);

      _jspx_page_context = pageContext;

      application = pageContext.getServletContext();

      config = pageContext.getServletConfig();

      session = pageContext.getSession();

      out = pageContext.getOut();

      _jspx_out = out;

 

    从这里开始,才是JSP页面的内容[崔8] 

   }…

 

3 pageContext对象

  在JavaWeb中一共四个域对象,其中Servlet中可以使用的是request、session、application三个对象,而在JSP中可以使用pageContext、request、session、application四个域对象。

pageContext 对象是PageContext类型,它的主要功能有:

l  域对象功能;

l  代理其它域对象功能;

l  获取其他内置对象;

 

3.1 域对象功能

  pageContext也是域对象,它的范围是当前页面。它的范围也是四个域对象中最小的!

l  void setAttribute(String name, Object value);

l  Object getAttrbiute(String name, Object value);

l  void removeAttribute(String name, Object value);

3.2 代理其它域对象功能

还可以使用pageContext来代理其它3个域对象的功能,也就是说可以使用pageContext向request、session、application对象中存取数据,例如:

 

    pageContext.setAttribute("x", "X");[崔9] 

    pageContext.setAttribute("x", "XX", PageContext.REQUEST_SCOPE);[崔10] 

    pageContext.setAttribute("x", "XXX", PageContext.SESSION_SCOPE);[崔11] 

    pageContext.setAttribute("x", "XXXX", PageContext.APPLICATION_SCOPE);[崔12] 

 

l  void setAttribute(String name, Object value, int scope):在指定范围中添加数据;

l  Object getAttribute(String name, int scope):获取指定范围的数据;

l  void removeAttribute(String name, int scope):移除指定范围的数据;

l  Object findAttribute(String name):依次在page、request、session、application范围查找名称为name的数据,如果找到就停止查找。这说明在这个范围内有相同名称的数据,那么page范围的优先级最高!

3.3 获取其他内置对象

一个pageContext对象等于所有内置对象,即1个当9个。这是因为可以使用pageContext对象获取其它8个内置对象:

l  JspWriter getOut():获取out内置对象;

l  ServletConfig getServletConfig():获取config内置对象;

l  Object getPage():获取page内置对象;

l  ServletRequest getRequest():获取request内置对象;

l  ServletResponse getResponse():获取response内置对象;

l  HttpSession getSession():获取session内置对象;

l  ServletContext getServletContext():获取application内置对象;

l  Exception getException():获取exception内置对象;

 


 [崔1]request和response两个对象是方法参数。

 [崔2]pageContext对象

 [崔3]session对象

 [崔4]application对象

 [崔5]config对象

 [崔6]out对象

 [崔7]page对象

 [崔8]JSP页面的内容从这里才开始,所以可以使用上面定义的变量!这就是9大内置对象为什么可以在JSP页面中无需创建就可以使用的奥秘了!

 [崔9]向pageContext中存储数据

 [崔10]向request中存储数据

 [崔11]向session中存储数据

 [崔12]向application中存储数据

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!