用代碼演示:JSP的4個作用域

1.JSP中的作用域有什麼用?

在WEB開發中,JSP作爲視圖進行數據展現,提供了4個不同的作用域用於在頁面中共享數據,這4個作用域的範圍已經生命週期各有不同,搞清楚4個作用域之間區別,方便我們實現不同的業務場景的功能。

2.哪4個作用域?

page域,request域,session域,application域

3.作用域之間的區別

3.1 pageContext對象

pageContext對象:它的生命週期即page域,指存儲在pageContext對象的數據只在當前頁面有效,當發生頁面跳轉時,則在pageContext域的數據進行銷燬

設值:

pageContext.setAttribute("Key","value");

取值:

pageContext.getAttribute("Key");

3.2 request對象

request對象:它的生命週期即request域,指當把數據存儲在request對象中,當頁面發生轉發或在當前頁面,都能拿到request域中的數據。當request請求結束,則存儲的數據進行銷燬。

設值:

request.setAttribute("key","value");

取值:

request.getAttribute("key");

3.3 session對象

session對象:它的生命週期即session域,session基於瀏覽器,指把數據存儲與session域中,當瀏覽器關閉時,則數據銷燬。

設值:

session.setAttrbute("key","value"); 

取值:

session.getAttribute("key");

3.4 application對象

application對象:它的生命週期即應用域,存在整個應用中。當把數據設置在application中,當服務器關閉則銷燬。
設值:

application.setAttribute("key","value");

取值:

application.getAttribute("key");

4. 代碼演示

4.1 演示page域

頁面:scope.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>作用域測試</title>
</head>
<body>
<%
	//設值
    pageContext.setAttribute("name1","zhangshan001");
    request.setAttribute("name2","zhangshan001");
    session.setAttribute("name3","zhangshan001");
    application.setAttribute("name4","zhangshan001");
	//取值
    String name1 = (String)pageContext.getAttribute("name1");
    String name2 = (String)request.getAttribute("name2");
    String name3 = (String)session.getAttribute("name3");
    String name4 = (String)application.getAttribute("name4");
%>

pageContext域:${name1}<br/>
request域:${name2}<br/>
session域:${name3}<br/>
application域:${name4}<br/>

</body>
</html>

訪問當前頁面,即可看到4個作用域都能拿到值,如下圖:
在這裏插入圖片描述

4.2 演示request域

演示request域,需要2個頁面。分別是scope.jsp與scope2.jsp。如下圖:在scope.jsp中設值,通過forward轉發到scope2.jsp後,發現pageContext值拿不到了,其他值都能拿到進行對比。

頁面:scope.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>作用域測試</title>
</head>
<body>
<%
    pageContext.setAttribute("name1","zhangshan001");
    request.setAttribute("name2","zhangshan001");
    session.setAttribute("name3","zhangshan001");
    application.setAttribute("name4","zhangshan001");
    String name1 = (String)pageContext.getAttribute("name1");
    String name2 = (String)request.getAttribute("name2");
    String name3 = (String)session.getAttribute("name3");
    String name4 = (String)application.getAttribute("name4");
%>
pageContext域:${name1}<br/>
request域:${name2}<br/>
session域:${name3}<br/>
application域:${name4}<br/>

<jsp:forward page="scope2.jsp"></jsp:forward>
</body>
</html>

頁面:scope2.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>作用域測試</title>
</head>
<body>
<%
    String name1 = (String)pageContext.getAttribute("name1");
    String name2 = (String)request.getAttribute("name2");
    String name3 = (String)session.getAttribute("name3");
    String name4 = (String)application.getAttribute("name4");
%>
<h1>scope2.jsp</h1>
pageContext域:${name1}<br/>
request域:${name2}<br/>
session域:${name3}<br/>
application域:${name4}<br/>
</body>
</html>

在這裏插入圖片描述

4.3演示session域

session是基於瀏覽器,換個瀏覽器訪問scope2.jsp,會發現session域的值拿不到了。(注意,是訪問scope2.jsp而不是scope.jsp。因爲scope.jsp會重新設值並取值。)
在這裏插入圖片描述

4.4 演示application域

由於application域是基於整個應用的。所以需要關閉tomcat服務器纔會銷燬。重啓tomcat後,如下圖:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章