用代码告诉你: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后,如下图: