zoukankan      html  css  js  c++  java
  • JSP九大内置对象

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

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

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

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

    pageContext(PageContext):页面上下文对象;

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

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

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

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

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

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

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

    通过真身来对照jsp

    在Tomcat中会把jsp变成Java文件,这个文件可以在Tomcat文件目录下的work文件下找到,在里面就已经创建了jsp九大内置对象,如下

    public void _jspService(HttpServletRequest request, HttpServletResponse response)
            throws java.io.IOException, ServletException {
    
        PageContext pageContext = null;
        HttpSession session = null;
        ServletContext application = null;
        ServletConfig config = null;
        JspWriter out = null;
        Object page = this;
        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页面的内容
       }…

    pageContext对象

    JavaWeb中一共四个域对象,其中Servlet中可以使用的是request、session、ServletContext三个对象,

    而在JSP中可以使用pageContext、request、session、application四个域对象。

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

    域对象功能;

    代理其它域对象功能;

    获取其他内置对象;

     

    域对象功能

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

    void setAttribute(String name, Object value);

    Object getAttrbiute(String name, Object value);

    void removeAttribute(String name, Object value)

    代理其它域对象功能

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

        pageContext.setAttribute("x", "X");

        pageContext.setAttribute("x", "XX", PageContext.REQUEST_SCOPE);

        pageContext.setAttribute("x", "XXX", PageContext.SESSION_SCOPE);

        pageContext.setAttribute("x", "XXXX", PageContext.APPLICATION_SCOPE);

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

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

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

    Object findAttribute(String name):依次在page、request、session、application范围查找名称为name的数据,如果找到就停止查找。

    这说明在这个范围内有相同名称的数据,那么page范围的优先级最高!


    获取其他内置对象

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

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

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

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

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

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

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

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

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

  • 相关阅读:
    Selenium2用最简xpath查找元素
    如何锁定Android系统CPU的频率
    github FATAL:unable to access 'https://github.com/...: Failed to connect to github.com:443; No error
    解压.tar.gz出错gzip: stdin: not in gzip format tar: /Child returned status 1 tar: Error is not recoverable: exiting now
    Shell脚本完成hadoop的集群安装
    Linux压缩与归档
    selenium2-元素管理方式及解析
    selenium2-框架思想介绍
    11.AutoMapper 之值转换器(Value Transformers)
    10.AutoMapper 之自定义值解析器(Custom Value Resolvers)
  • 原文地址:https://www.cnblogs.com/QianYue111/p/9745352.html
Copyright © 2011-2022 走看看