JSP内置对象

-------------------JSP内置对象-------------------------------

JSP内置对象

  • 1 什么是内置对象(面试常问)

内置对象是在JSP页面中无需创建就可以直接使用的变量。在JSP中一共有9个这样的对象!它们分别是:

  1. out(JspWriter);
  2. config(ServletConfig);
  3. page(当前JSP的真身类型);
  4. pageContext(PageContext);
  5. exception(Throwable);
  6. request(HttpServletRequest);
  7. response(HttpServletResponse);
  8. application(ServletContext);
  9. Session     (HttpSession)。

 

  • 2 隐藏对象概述
  1. out:最为常用的方法是print(),向页面输出!它与response.getWriter()基本相同!
  2. config:在页面中基本没有什么用,但如果JSP在web.xml中存在配置,而且存在初始化参数,那么可以使用config来获取;config对象的功能是:getServletContext()、getServletName()、getInitParameter(),这三个方法在JSP中都没什么用。所以config就没什么用了。JSP也可以在web.xml中配置,但我们没有配置它的必要!
  3. page:基本没用!表示当前JSP页面的当前实例!在页面中使用this和使用page是相同的;也没什么用。
  4. request:与Servlet中的request一样,没有区别;
  5. response:与Servlet中的response一样,没有区别;
  6. application:就是ServletContext对象;
  7. session:就是HttpSession对象;
  8. exception:虽然名字叫exception,但类型为Throwable。它只能在错误页中可以使用!后面讲
  9. pageContext:很有用的东西,下面会讲解。你以后可能不会经常直接使用它,但一定间接使用它
  • 3 对照JSP真身查看内置对象

我们发现,在JSP中的内容会出现在“真身”的_jspService()方法中,而且在_jspService()方法上方还有一些其实代码:

  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页面的内容

   }…

 

 

JSP中的内容都出现在try块中,而且在try块的正文!上方是对隐藏对象的初始化!!!

上面代码只是给出普通页面的“真身”,而错误页面的“真身”你会看到exception对象的出现。

 

  • 4 JSP四个域对象

 

    • 域的范围和属性管理

 

    1. pageContext范围; 当前页面之内有效
  •   <body>
  •       <%
  •              request.setCharacterEncoding("UTF-8");
  •              response.setContentType("text/html;charset=UTF-8");
  •              response.setCharacterEncoding("UTF-8");
  •              //pageContext的作用域在当前页面内有效
  •              pageContext.setAttribute("name", "李昆鹏");
  •              pageContext.setAttribute("birthday", new Date());
  •             
  •              String name=(String)pageContext.getAttribute("name");
  •              Date birthday=(Date)pageContext.getAttribute("birthday");
  •        %>
  •        <h1>name:  <%=name %></h1>
  •        <h1>birthday:  <%=birthday %></h1>
  •   </body>
    1. request范围;当前的请求内有效
  •   <body>
  •       <%
  •              //request的作用域在当前请求内有效
  •              request.setAttribute("name", "李昆鹏");
  •              request.setAttribute("birthday", new Date());
  •             
  •              String name=(String)request.getAttribute("name");
  •              Date birthday=(Date)request.getAttribute("birthday");
  •        %>
  •        <%-- jsp中服务端的页面跳转 --%>
  •        <jsp:forward page="/success.jsp"></jsp:forward>
  •  </body>
    1. session范围;当前的会话内有效
  •   <body>
  •       <%
  •              //session的作用域在当前这次会话内(只要浏览器不关闭)有效
  •              session.setAttribute("name", "李昆鹏");
  •              session.setAttribute("birthday", new Date());
  •             
  •              String name=(String)session.getAttribute("name");
  •              Date birthday=(Date)session.getAttribute("birthday");
  •        %>
  •        <h1>name:  <%=name %></h1>
  •        <h1>birthday:  <%=birthday %></h1>
  •   </body>
    1. application范围;当前这次服务器生命周期内有效
  •   <body>
  •       <%
  •              //application的作用域在当前所属服务器生命周期内有效
  •              application.setAttribute("name", "李昆鹏");
  •              application.setAttribute("birthday", new Date());
  •             
  •              String name=(String)application.getAttribute("name");
  •              Date birthday=(Date)application.getAttribute("birthday");
  •        %>
  •        <h1>name:  <%=name %></h1>
  •        <h1>birthday:  <%=birthday %></h1>
  •   </body>

       域对象的共同特点是都管理域中的属性:

              他们有着相同的方法

  • void setAttribute(String name, Object value);
  • Object getAttrbiute(String name, Object value);
  • void removeAttribute(String name, Object value);

 

    • pageContext详细分析

pageContext 对象是PageContext类型,它不只是域对象,而且还可以操作所有域对象,还可以获取其他隐藏对象。

  1. 本身是域对象:pageContext是JSP中的域对象,而在Servlet中不能使用它!它表示的当前页面中可以使用,是最小的范围!
  • void setAttribute(String name, Object value);
  • Object getAttrbiute(String name, Object value);
  • void removeAttribute(String name, Object value);
  1. 操作所有域(四个域):可以使用pageContext对象操作所有域对象,在getAttribute()、setAttribute()、removeAttribute()三个方法中多添加一个参数,int scope来指定范围。在PageContext类中包含四个int类型的常量表示四个范围:
  • PAGE_SCOPE:pageContext范围;
  • REQUEST_SCOPE:request范围;
  • SESSION_SCOPE:session范围;
  • APPLICATION_SCOPE:application范围;
  • void setAttribute(String name, Object value, int scope):设置数据到指定的范围中,例如:pageContext.setAttribute(“hello”, “hello world”, PageContext.REQUEST),这个方法调用等同与:request.setAttribute(“hello”, “hello world”);
  • Object getAttribute(String name, int scope):获取指定范围的数据;
  • void removeAttribute(String name, int scope):移除指定范围的数据;
  • Object findAttribute(String name):在所有范围内查找数据,依次为page、request、session、application。如果在前一个范围内查找到数据,那么就返回,而不会在到下一个范围中去查找!
  1. 获取其他隐藏对象:可以使用pageContext获取其他隐藏对象。
  • JspWriter getOut():获取out隐藏对象;
  • ServletConfig getServletConfig():获取config隐藏对象;
  • Object getPage():获取page隐藏对象;
  • HttpServletRequest  getRequest():获取request隐藏对象;
  • HttpServletResponse getResponse:获取response隐藏对象;
  • HttpSession getSession():获取session隐藏对象;
  • ServletContext getServletContext():获取application隐藏对象;
  • JspException getException():获取exception隐藏对象转换后的JspException对象。

 

  <body>

      <%

             //设置pageContext属性的作用范围为request

             pageContext.setAttribute("name", "李昆鹏",PageContext.REQUEST_SCOPE);

             pageContext.setAttribute("birthday", new Date(),PageContext.REQUEST_SCOPE);

            

             String name=(String)pageContext.getAttribute("name",PageContext.REQUEST_SCOPE);

             Date birthday=(Date)pageContext.getAttribute("birthday",PageContext.REQUEST_SCOPE);

            

             //获得项目名,比较常用

             String path1 = pageContext.getServletContext().getContextPath();

             // /jsp_demo1

       %>

       <h1>name:  <%=name %></h1>

       <h1>birthday:  <%=birthday %></h1>

  </body>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值