JSP——四大作用域

pageContext
作用于当前页面 生命周期太短,不常用

request
作用于一次请求
1,ajax 不会打断一次请请求
2,JSP:forword不会打断一次请求
3,服务器内部的跳转不会打断一次请求

1,a标签会打断一次请求
2,用户执行的操作引起页面跳转会打断一次请求

session
作用于一次会话 会话有时间的限制

application
作用于整个服务器,如不关闭服务器一直存在

例子:
A.jsp

<%@page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%
  pageContext.setAttribute("pageContextKey", Calendar.getInstance());

  request.setAttribute("requestKey", Calendar.getInstance());
  
  session.setAttribute("sessionKey", Calendar.getInstance());
  
  application.setAttribute("applicationKey", Calendar.getInstance());
%>
<%
	out.print(pageContext.getAttribute("pageContextKey"));
	out.print("<br />");
	out.print(request.getAttribute("requestKey"));
	out.print("<br />");
	out.print(session.getAttribute("sessionKey"));
	out.print("<br />");
	out.print(application.getAttribute("applicationKey"));
%>

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

B.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% 
	out.print(pageContext.getAttribute("pageContextKey"));
	out.print("<br />");
	out.print(request.getAttribute("requestKey"));
	out.print("<br />");
	out.print(session.getAttribute("sessionKey"));
	out.print("<br />");
	out.print(application.getAttribute("applicationKey"));
%>
<a href="C.jsp">GOGOGOGOGOGOGOGOGOGOGOGOGOGOGOGOGO</a>
</body>
</html>

C.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% 
	out.print(pageContext.getAttribute("pageContextKey"));
	out.print("<br />");
	out.print(request.getAttribute("requestKey"));
	out.print("<br />");
	out.print(session.getAttribute("sessionKey"));
	out.print("<br />");
	out.print(application.getAttribute("applicationKey"));
%>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值