page:当前页面,也就是只要挑到别的页面就失效了,可以近似理解为java的this对象
request:一次会话,简单的理解就是一次请求范围内有效,例如如果通过forward方式跳转,则forward目标页面仍然可以拿到request中的属性值,再例如如果通过redirect方式进行页面跳转,由于redirect相当于重新发出的请求,request中的值会消失
session:浏览器进程,只要当前页面没有被关闭(没有被浏览器强制清除),不管怎么跳转都是有效的(也可以说是存在一个会话周期中)
application:服务器,只要服务器没有重启(没有被程序强制清除),数据就有效
pageScope.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<!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>
<body>
<%
//设置两个page范围的数据key->value
pageContext.setAttribute("name","page_barry");
pageContext.setAttribute("age",21);
%>
<%
//取值
String name=(String)pageContext.getAttribute("name");
int age=(Integer)pageContext.getAttribute("age");
%>
<font>姓 名:<%=name %></font>
<font>年 龄:<%=age %></font>
</body>
</html>
pageContext01.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<%@ page errorPage="error.jsp" %>
<!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>
<%
pageContext.setAttribute("name0","pageInfo");
request.setAttribute("name1","requestInfo");
session.setAttribute("name2","sessionInfo");
application.setAttribute("name3","applicationInfo");
out.println("使用pageContext<br/>");
out.println("page中的属性值:"+pageContext.getAttribute("name0")+"<br/>");
out.println("request中的属性值:"+pageContext.getRequest().getAttribute("name1")+"<br/>");
out.println("session中的属性值:"+pageContext.getSession().getAttribute("name2")+"<br/>");
out.println("application中的属性值:"+pageContext.getServletContext().getAttribute("name3")+"<br/>");
%>
</body>
</html>
pageScope_2.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<!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>
<body>
<%
//设置两个page范围的数据key->value
pageContext.setAttribute("name","page_barry");
pageContext.setAttribute("age",21);
%>
<jsp:forward page="pageTarget_2.jsp"></jsp:forward>
</body>
</html>
pageTarget_2.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<!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>
<body>
<%
//取值
String name=(String)pageContext.getAttribute("name");
int age=(Integer)pageContext.getAttribute("age");
%>
<font>姓 名:<%=name %></font>
<font>年 龄:<%=age %></font>
</body>
</html>
requestScope.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>
<%
//设置两个request范围的数据key->value
request.setAttribute("name","request_barry");
request.setAttribute("age",12);
%>
<jsp:forward page="requestTarget.jsp"></jsp:forward>
</body>
</html>
requestTarget.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<!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>
<%
//取值
String name=(String)request.getAttribute("name");
int age=(Integer)request.getAttribute("age");
//获取头信息
Enumeration enu=request.getHeaderNames();
while(enu.hasMoreElements()){
String headerName=(String)enu.nextElement();
String headerValue=request.getHeader(headerName);
%>
<h4><%=headerName %> <%=headerValue %></h4>
<%
}
%>
<font>姓名<%=name %></font><br>
<font>年龄<%=age %></font>
</body>
</html>
requestScope_2.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>
<%
//设置两个request范围的数据key->value
request.setAttribute("name","request_barry");
request.setAttribute("age",12);
%>
<%response.sendRedirect("requestTarget_2"); %>
</body>
</html>
requestTarget_2.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<!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>
<%
//取值
String name=(String)request.getAttribute("name");
int age=(Integer)request.getAttribute("age");
//获取头信息
Enumeration enu=request.getHeaderNames();
while(enu.hasMoreElements()){
String headerName=(String)enu.nextElement();
String headerValue=request.getHeader(headerName);
%>
<h4><%=headerName %> <%=headerValue %></h4>
<%
}
%>
<font>姓名<%=name %></font><br>
<font>年龄<%=age %></font>
</body>
</html>
sessionScope.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>
<%
//设置两个session范围的数据key->value
session.setAttribute("name","session_barry");
session.setAttribute("age",12);
%>
<h1>session值设置完毕!</h1>
</body>
</html>
sessionTarget.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>
<%
String name=(String)session.getAttribute("name");
Integer age=(Integer)session.getAttribute("age");
%>
<font>姓名<%=name %></font><br>
<font>年龄<%=age %></font>
</body>
</html>
applicationScope.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>
<%
//设置两个application范围的数据key->value
application.setAttribute("name","application_barry");
application.setAttribute("age",12);
%>
<h1>application值设置完毕!</h1>
</body>
</html>
applicationTarget.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>
<%
//取值
String name=(String)application.getAttribute("name");
int age=(Integer)application.getAttribute("age");
%>
<font>姓名<%=name %></font><br>
<font>年龄<%=age %></font>
</body>
</html>