JSP的四大作用域的存储和获取数据的方式一样,差别在于取值的范围不同。
四大域各自作用范围为:
pageContext:当前JSP页面有效
request:请求有效
session:会话有效(关闭浏览器则失效)
application:整个Web应用有效(服务器关闭或者重启则失效)
1、pageContext作用域
(pageContextScope:键值,设值:李四)
由于输出脚本中的a是字符类型,所以需要对pageContext.getAttribute进行强制类型转换String a = (String)来解决输出脚本a的报错
jsp代码:创建一个jsp页面,在body标签里面书写以下代码
<%
pageContext.setAttribute("pageContextScope", "李四");
String a = (String) pageContext.getAttribute("pageContextScope");
%>
servlet完整代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>pageConext作用域</title>
</head>
<body>
<%
pageContext.setAttribute("pageContextScope", "李四");
String a = (String) pageContext.getAttribute("pageContextScope");
%>
<h2>
pageContextScope:<%=a%>
</h2>
</body>
</html>
输出结果:
2、request作用域
创建一个servlet(ScopeServlet.java),在servlet里存值
转发操作(可以获取到值)
getRequestDispatcher:转发到页面;
forward:传递reqest,response
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("requestScope","李二");
req.getRequestDispatcher("/jsp/object/scope.jsp").forward(req, resp);
}
req.getRequestDispatcher("转发路径"),我的转发jsp页面位于:webapp下的jsp/object文件夹下的scope.jsp
jsp页面代码:
<h4>request域</h4>
<%
String b = (String) request.getAttribute("requestScope");
%>
requestScope:<%=b%>
注:由于Servlet发生变化,需要重新部署项目;
由于request是在servlet上存值,所以需要在ScopeServlet上启动;
输出结果:
重定向操作(获取不到值)
Servlet代码:
req.setAttribute("requestScope","李二");
resp.sendRedirect(req.getContextPath()+"/jsp/object/scope.jsp");
jsp代码:
<h4>request域</h4>
<%
String b = (String) request.getAttribute("requestScope");
%>
requestScope:<%=b%>
输出结果:
注:观察两者的区别
转发操作:地址栏没有发生变化
重定向操作:地址栏发生变化
3、session作用域
servlet代码:
req.getSession().setAttribute("sessionScope", "李三");
jsp代码:
<h4>session域</h4>
<%
String c = (String)session.getAttribute("sessionScope");
%>
session:<%=c %>
输出结果:
4、application作用域
servlet代码:
注:servlet这里的作用域不是application而是servletContextScope,因为application对应的servlet类型是javax.servlet.servletContext
req.getServletContext().setAttribute("servletContextScope", "李四");
jsp代码:
注:
<h4>application域</h4>
<%
String d = (String) application.getAttribute("servletContextScope");
%>
application:<%=d %>
输出结果:
完整代码:
servlet:
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="ScopeServlet",value="/scopeServlet")
public class ScopeServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("requestScope","李二");
//req.getRequestDispatcher("/jsp/object/scope.jsp").forward(req, resp);
//转发
//req.getRequestDispatcher("/jsp/object/scope.jsp").include(req, resp);
//重定向
resp.sendRedirect(req.getContextPath()+"/jsp/object/scope.jsp");
req.getSession().setAttribute("sessionScope", "李三");
req.getServletContext().setAttribute("servletContextScope", "李四");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>pageConext\request\session\application四大作用域对象</title>
</head>
<body>
<h4>pageContext域</h4>
<%
pageContext.setAttribute("pageContextScope", "李一");
String a = (String) pageContext.getAttribute("pageContextScope");
%>
pageContextScope:<%=a%>
<h4>request域</h4>
<%
String b = (String) request.getAttribute("requestScope");
%>
requestScope:<%=b%>
<h4>session域</h4>
<%
String c = (String) session.getAttribute("sessionScope");
%>
session:<%=c%>
<h4>application域</h4>
<%
String d = (String) application.getAttribute("servletContextScope");
%>
application:<%=d %>
</body>
</html>
复制地址栏的地址,关闭所有开启的浏览器页面,重新打开浏览器,粘贴地址回车,你会发现,session域也取不到值
仅供参考,如有不足之处,敬请见谅。