4种范围对象的作用域(从小到大排序)
a.pageContext:当前页面有效,页面跳转后失效
b.resquest:同一次请求有效,若请求转发方式修改为重定向(要进行两次请求)则无效
c.session:同一次会话有效,无论页面的跳转方式是请求转发还是重定向,关闭或切换浏览器后无效
d.application:整个项目运行期间都有效,切换浏览器仍然有效,关闭服务或切换其它项目后无效
1.以上4个范围对象,通过setAttribute()赋值,通过getAttribute()取值
2.以上范围对象,尽量使用最小的范围,因为范围越大则损耗越大
应用实例:4种范围对象的作用域
pageContext.jsp
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//pageContext作用域
//当前页面有效,页面跳转后失效
pageContext.setAttribute("hello", "world");
request.getRequestDispatcher("pc1.jsp").forward(request,response);
%>
</body>
</html>
pc1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
pc1.jsp<br>
<%=pageContext.getAttribute("hello")%>
</body>
</html>
request.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//request作用域
//同一次请求有效,若请求转发方式修改为重定向(要进行两次请求)则无效
request.setAttribute("hello", "world");
//request.getRequestDispatcher("rq1.jsp").forward(request,response);
response.sendRedirect("rq1.jsp");
%>
</body>
</html>
rq1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
rq1.jsp<br>
<%=request.getAttribute("hello")%>
</body>
</html>
session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//session作用域
//同一次会话有效,无论页面的跳转方式是请求转发还是重定向,关闭或切换浏览器后无效:登录-->退出
session.setAttribute("hello", "world");
//request.getRequestDispatcher("ss1.jsp").forward(request,response);
response.sendRedirect("ss1.jsp");
%>
</body>
</html>
ss1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
ss1.jsp<br>
<%=session.getAttribute("hello") %>
</body>
</html>
application.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//application作用域
//整个项目运行期间都有效,切换浏览器仍然有效,关闭服务或切换其它项目后无效
application.setAttribute("hello", "world");
//request.getRequestDispatcher("ap1.jsp").forward(request,response);
response.sendRedirect("ap1.jsp");
%>
</body>
</html>
ap1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
ap1.jsp<br>
<%=application.getAttribute("hello")%>
</body>
</html>
本文介绍了Java Web中四种范围对象的作用域:pageContext、request、session和application。按照作用域大小排序,pageContext仅限当前页面,request在同一次请求内有效,session适用于同一次会话,而application在整个项目运行期间有效。建议使用最小作用域以减少资源损耗。并提供了应用实例:pageContext.jsp、pc1.jsp、request.jsp、rq1.jsp、session.jsp、ss1.jsp、application.jsp和ap1.jsp。
&spm=1001.2101.3001.5002&articleId=111168226&d=1&t=3&u=47b44c8a62074d158e7bc95363ee0065)
1101

被折叠的 条评论
为什么被折叠?



