(1)四种属性范围
(2)九个内置对象
? ? 在我们之前的操作当中,经常使用到request.getParameter()来接受上一个页面传递过来的参数,那么在java当中,如果要使用一个对象,必须要先实例化,但是我们的request却一直没有实例化过就可以直接使用,这意味着这个对象在web容器当中本身就已经实例化了,这种对象我们叫做内置对象。
NO. | 内置对象 | 类型 |
1 | pageContext | javax.servlet.jsp.PageContext |
2 | request | javax.servlet.http.HttpServeltRequest |
3 | response | javax.servlet.http.HttpServletResponse |
4 | session | javax.servlet.http.HttpSession |
5 | application | javax.servlet.ServlertContext |
6 | config | javax.servlet.SerletConfig |
7 | out | javax.servlet.jsp.jspWriter |
8 | page | java.lang.Object |
9 | exception | java.lang.Throwable |
属性范围:(四种会话跟踪技术)
所谓的属性范围,就是指在一个页面中设置了属性之后,是否能够在其他页面进行访问(是否有这种权限)。
四种属性范围分别是:
第一种属性范围:
????? page属性范围:(pageContext)属性范围:
???????? 这个属性范围非常好理解,指的就是本页面访问本页面设置的属性。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%
pageContext.setAttribute("name","page属性范围");
pageContext.setAttribute("date",new Date());
%>
<h1>
<%=(String)pageContext.getAttribute("name")%>
<%=(Date)pageContext.getAttribute("date") %>
</h1>
</body>
</html>
下面进行一次服务端跳转(jsp:forward)验证跳转后的页面无法访问前一个页面的page属性。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%
pageContext.setAttribute("name","page属性范围");
pageContext.setAttribute("date",new Date());
%>
<jsp:forward page="forwordtonext.jsp"></jsp:forward>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h1>
<%=(String)pageContext.getAttribute("name")%>
<%=(Date)pageContext.getAttribute("date") %>
</h1>
</body>
</html>
第二种属性范围:request属性范围
Request属性范围指的就是在服务端跳转中生效的属性范围。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%
request.setAttribute("name","page属性范围");
request.setAttribute("date",new Date());
%>
<jsp:forward page="forwordtonext.jsp"></jsp:forward>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h1>
<%=(String)request.getAttribute("name")%>
<%=(Date)request.getAttribute("date") %>
</h1>
</body>
</html>
以下验证客户端跳转是否能够访问:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%
request.setAttribute("name","page属性范围");
request.setAttribute("date",new Date());
%>
<a href="forwordtonext.jsp"><h1>客户端跳转</h1></a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h1>
<%=(String)request.getAttribute("name")%>
<%=(Date)request.getAttribute("date") %>
</h1>
</body>
</html>
总结一下目前学到的跳转形式:
???? 客户端跳转:
???????? 第一种是表单的提交
???????? 第二种是<a>标签超链接跳转。
???? 服务端跳转:
???????? jsp:forward
第三种属性范围:
?????? Session范围:
?????? 这种属性范围,一旦设置好,只要在一个浏览器未关闭的情况下都可以访问,但是这只针对一个浏览器的一个用户而言。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%
session.setAttribute("name","page属性范围");
session.setAttribute("date",new Date());
%>
<a href="forwordtonext.jsp"><h1>客户端跳转</h1></a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h1>
<%=(String)session.getAttribute("name")%>
<%=(Date)session.getAttribute("date") %>
</h1>
</body>
</html>
以上验证了客户端跳转之后,session范围的属性依旧可以访问。
重新开启一个浏览器直接访问跳转后页面,发现属性无法访问,验证了session范围只针对一个浏览器一个用户而言。
重启浏览器,直接访问跳转后页面,发现属性也无法访问,验证了属性只保存在一次会话范围,想要再次访问就需要再次设置。
第四种属性范围:
???? Application属性范围,指的是在服务器上面设置属性,一旦设置,用户可以随时随地范围,除非服务器关闭或者重启。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%
application.setAttribute("name","page属性范围");
application.setAttribute("date",new Date());
%>
<a href="forwordtonext.jsp"><h1>客户端跳转</h1></a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h1>
<%=(String)application.getAttribute("name")%>
<%=(Date)application.getAttribute("date") %>
</h1>
</body>
</html>
? ? 四种属性范围互相不冲突,独立工作,但是如果在不同的属性范围里存在着键名一样的属性的话,访问的顺序如下page范围—》request范围—》session范围—》application范围。