標籤:
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>
jsp中的四種對象範圍