javaweb學習18:JSP內建物件及作用域
阿新 • • 發佈:2022-03-28
-
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);
}
}
-
程式碼案例:重要
<%
-
程式碼案例2:分析作用域底層
<%
-
程式碼案例:頁面跳轉
<%