Demonstrate with code: 4 scopes of JSP

1. What is the use of scope in JSP?

In WEB development, JSP is used as a view to display data, and it provides 4 different scopes for sharing data in the page. The scope of these 4 scopes has different life cycles. Find out between the 4 scopes. The difference is convenient for us to realize the functions of different business scenarios.

2. Which 4 scopes?

page domain, request domain, session domain, application domain

3. The difference between scopes

3.1 pageContext object

pageContext object: its life cycle is the page domain, which means that the data stored in the pageContext object is only valid on the current page. When a page jump occurs, the data in the pageContext domain is destroyed

Set value:

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

Value:

pageContext.getAttribute("Key");

3.2 request object

Request object: its life cycle is the request domain, which means that when data is stored in the request object, when the page is forwarded or on the current page, the data in the request domain can be obtained. When the request is over, the stored data is destroyed.

Set value:

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

Value:

request.getAttribute("key");

3.3 session object

Session object: its life cycle is the session domain. The session is based on the browser, which means that data is stored in the session domain. When the browser is closed, the data is destroyed.

Set value:

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

Value:

session.getAttribute("key");

3.4 application object

Application object: Its life cycle is the application domain, which exists in the entire application. When the data is set in the application, it will be destroyed when the server is closed.
Set value:

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

Value:

application.getAttribute("key");

4. Code Demo

4.1 Demo page domain

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>

Visit the current page, you can see that all 4 scopes can get values, as shown below:
Insert picture description here

4.2 Demo request domain

To demonstrate the request domain, 2 pages are required. They are scope.jsp and scope2.jsp. As shown in the figure below: Set the value in scope.jsp and forward it to scope2.jsp through forward. It is found that the pageContext value cannot be obtained, and other values ​​can be obtained for comparison.

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/>

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

Page: 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>

Insert picture description here

4.3 Demo session domain

The session is based on the browser. If you visit scope2.jsp from another browser, you will find that the value of the session field is not available. (Note that it is access to scope2.jsp instead of scope.jsp. Because scope.jsp will reset and take the value.)
Insert picture description here

4.4 Demo application domain

Because the application domain is based on the entire application. So you need to close the tomcat server to destroy it. After restarting tomcat, as shown below:
Insert picture description here

Guess you like

Origin blog.csdn.net/u010312671/article/details/106880716