@TOC
pageContext对象
pageContext对象提供了对JSP页面内所有的对象及名字空间的访问,我们可以使用这个对象在JSP页面内存储和调用各种各样的数据
比如 : ${pageContext.request.contextPath}
这个就是我们利用pageContext来获取现在访问的项目名称,它能帮助我们绝对定位到项目的根目录下,在配置一些请求或者引入文件的位置的时候这个应用还是非常多的;
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/css/bootstrap.min.css">
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/bootstrap.js"></script>
pageContext 除了能够帮助我们获取对象数据之外,还能够保存数据信息:
<%
pageContext.setAttribute("msg", "Hello,PageContext!");
%>
<div>
<h1>${msg}</h1>
</div>
效果:
注:pageContext保存的数据只有在当前页面有效,无法通过请求转发在另一个页面使用当前页面保存的数据
request对象
与pageContext对象类似,request对象也可以保存数据,区别是request对象保存的数据可以通过请求转发在其他页面使用
<%
pageContext.setAttribute("msg", "Hello,PageContext!");
request.setAttribute("msg1", "Hello,Request!");
request.getRequestDispatcher("page1.jsp").forward(request, response);
%>
page1.jsp:
<div>
<h1>${msg}</h1>
<h1>${msg1}</h1>
</div>
效果:
通过上面的案例我们不难发现,pageContext的存值范围只有当前页面,所以它的作用域非常小;
而request作用域pageContext大一点,能够维持在一次转发链(转发链:一次请求在多个页面进行转发)的范围内
session对象
request对象保存的数据只能通过请求转发的方式传递,如果我们使用重定向,request内的数据就获取不到了,要解决这个问题,我们可以使用sesion对象
sesion对象也叫会话,一个会话就是在一段时间内,一个客户端与Web服务器的一连串相关的交互过程
我们与服务器的一次通信,其中会包含多个页面的跳转,里面就包括转发与重定向,而不管多少次转发与重定向其实服务器就只给我们创建一个会话叫做session对象;
session对象也是jsp的内置对象之一,是服务器创建用于与浏览器沟通的通道,类似我们数据库连接一样,所以我们如果把值存到这里那么肯定就能解决上面的问题;
|方法名称|说明 |
|--|--|
|String getId() | 获取sessionid |
|void setMaxInactiveInterval(int interval) | 设定session的存活时间(单位为秒) |
|int getInactiveInterval() | 获取sessionid的存活时间 |
|void invaildate |使session失效 |
|void setAttribute(String key,Object value) | 存值 |
|Object getAttribute(String key) | 通过key获取对象值|
|void removeAttribute(String key)| 从session中删除指定key的对象|
案例:
在index.jsp中存值,请求转发到page1.jsp
<%
pageContext.setAttribute("msg", "Hello,PageContext!");
request.setAttribute("msg1", "Hello,Request!");
session.setAttribute("msg2", "Hello,Session!");
request.getRequestDispatcher("page1.jsp").forward(request, response);
%>
page1用重定向的方式向page2发起请求
<%
response.sendRedirect("page2.jsp");
%>
page2.jsp
<div>
<h1>${msg}</h1>
<h1>${msg1}</h1>
<h1>${msg2}</h1>
</div>
能到达page2的就只有session里保存的数据;但是需要注意一个问题,session保存的数据也是有作用范围的,它的数据作用只能在本次浏览器的打开与关闭之间
Application 对象
application对象类似于系统的“全局变量”,在tomcat开启的时候,创建一个application对象,然后整个项目不管打开和关闭多少次都只有这一个对象,只要服务器不重启,application保存的数据就一直存在
|方法名称|说明 |
|--|--|
|void setAttribute(String key,Object value) | 存值 |
|Object getAttribute(String key) | 通过key获取对象值|
|String getRealPath(String path) | 返回相对路径的真实路径|
案例:
<%
pageContext.setAttribute("msg", "Hello,PageContext!");
request.setAttribute("msg1", "Hello,Request!");
session.setAttribute("msg2", "Hello,Session!");
application.setAttribute("msg3", "Hello,Application!");
request.getRequestDispatcher("page1.jsp").forward(request, response);
%>
page1.jsp
<%
response.sendRedirect("page2.jsp");
%>
page2.jsp
<div>
<h1>${msg}</h1>
<h1>${msg1}</h1>
<h1>${msg2}</h1>
<h1>${msg3}</h1>
</div>
测试结果:
关闭浏览器再访问page2.jsp 测试结果
基于application的特点,我们可以使用application对象保存网站的登录次数,只要tomcat不重启累计的登录次数都是有效的
Q.E.D.