1. 程式人生 > 其它 >javaweb學習18:JSP內建物件及作用域

javaweb學習18:JSP內建物件及作用域

javaweb學習18:JSP內建物件及作用域

  • JSP:9大內建物件

    • PageContext:存東西

    • Request:存東西

    • Response

    • Session:存東西

    • Application:【ServletContext】:存東西

    • config:【ServletConfig】

    • out

    • page:幾乎不用

    • Exception:跟Java異常一樣;

 

  • 使用場景:

    • Request:客戶端向伺服器傳送請求,產生的資料,使用者看完就沒用了,比如:新聞;

    • Session:客戶端向伺服器傳送請求,產生的資料,使用者用完一會還有用,比如:購物車;Hystrix

    • Application:客戶端向伺服器傳送請求,產生的資料,一個使用者用完了,其他使用者還可能使用;比如聊天資料;

 

  • 總結:

    • 引數的作用域範圍:參考:Java雙親委派機制;

    • JVM:雙親委派機制;

 

  • 分析程式碼:pageContext.setAttribute( );

    //PageContext類:
    public static final int PAGE_SCOPE = 1;
    public static final int REQUEST_SCOPE = 2;
    public static final int SESSION_SCOPE = 3;
    public static final int APPLICATION_SCOPE = 4;

    //setAttribute原始碼
    public void setAttribute(String name, Object attribute, int scope) {
       switch(scope) {
           case 1:
               this.mPage.put(name, attribute);
               break;
           case 2:
               this.mRequest.put(name, attribute);
               break;
           case 3:
               this.mSession.put(name, attribute);
               break;
           case 4:
               this.mApp.put(name, attribute);
               break;
           default:
               throw new IllegalArgumentException("Bad scope " + scope);
      }

    }

     

 

  • 程式碼案例:重要

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
       <title>Title</title>
    </head>
    <body>

    <%--內建物件:一般用來存東西--%>

    <%
       pageContext.setAttribute("name1","張三1");//儲存的資料只在一個頁面中有效;
       request.setAttribute("name2","張三2");//儲存的資料只在一次請求中有效;(請求轉發會攜帶整個引數)
       session.setAttribute("name3","張三3");//儲存的資料只在一次會話中有效;從開啟瀏覽器到關閉瀏覽器;
       application.setAttribute("name4","張三4");//儲存的資料只在伺服器中有效;從開啟伺服器到關閉伺服器;
    %>

    <%--指令碼片段中的程式碼,會被原封不動的生成到.JSP.java中 --%>
    <%

       //通過pageContext取出,我們通過尋找的方式來 findAttribute
       //從底層到高層(作用域):page-->reques-->session-->application
       //JVM:雙親委派機制:
       String name1 = (String)pageContext.findAttribute("name1");
       String name2 = (String)pageContext.findAttribute("name2");
       String name3 = (String)pageContext.findAttribute("name3");
       String name4 = (String)pageContext.findAttribute("name4");
       String name5 = (String)pageContext.findAttribute("name5");//不存在

    %>

    <%--找到之後怎麼輸出:使用EL表示式輸出: ${ }
       EL表示式:${ } 等價於  JSP表示式:<%= %>
    --%>

    <h1>取出的值為:</h1>
    <h3>${name1}</h3>
    <h3>${name2}</h3>
    <h3>${name3}</h3>
    <h3>${name4}</h3>
    <h3>${name5}</h3>

    <%-- null --%>
    <h3><%= name5%></h3>

    </body>
    </html>

     

  • 程式碼案例2:分析作用域底層

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
       <title>Title</title>
    </head>
    <body>
       
    <%
       pageContext.setAttribute("hello1","hello1",PageContext.SESSION_SCOPE);
       //session.setAttribute("hello1","hello1");

    %>


    </body>
    </html>

     

  • 程式碼案例:頁面跳轉

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
       <title>Title</title>
    </head>
    <body>

    <%
       //實現頁面轉發:雖然頁面跳轉,但是位址列不變
       pageContext.forward("/index.jsp");
       //request.getRequestDispatcher("/index.jsp").forward(request,response);

    %>


    </body>
    </html>