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{@Overrideprotected 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", "李四");}@Overrideprotected 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域也取不到值

仅供参考,如有不足之处,敬请见谅。

JSP之四大作用域(pageContext,request,session,application)相关推荐

  1. JSP中四大作用域和九大内置对象

    文章目录 九大内置对象 一.out对象 二.request对象 三.response对象 四.config对象 五.session对象 六.application对象 七.page对象 八.pageC ...

  2. html四大作用域,jsp的四大作用域是什么

    jsp的四大作用域是:1.application 作用域.2.session作用域.3.request作用域.4.page作用域. 1.application 作用域 假如将变量放在applicati ...

  3. 暑期项目经验(九) -- request session application

    request.session.application 一.基础知识 可以看看  浅谈:request,session,application (http://blog.csdn.net/hzc543 ...

  4. Jsp的四大作用域与九大对象

    内置对象特点: 1. 由JSP规范提供,不用编写者实例化. 2. 通过Web容器实现和管理 3. 所有JSP页面均可使用 4. 只有在脚本元素的表达式或代码段中才可使用(<%=使用内置对象%&g ...

  5. JSP的四大作用域和九大内置对象

    JSP的四大作用域和九大内置对象 https://wenku.baidu.com/view/a0974190152ded630b1c59eef8c75fbfc77d949d?ivk_sa=102319 ...

  6. JSP的四大作用域及属性范围

    1.JSP为什么要给出四大作用域 为了根据不同的情况去进行数据的存储.传递等,JSP给出了四大作用域以满足不同的使用情况 作用域 描述 pageContext 当前页生效 request 一次请求中生 ...

  7. JSP中四大作用域详解

    在学习时读到一篇好文章,分享给大家~~ 转自https://www.cnblogs.com/WindSun/p/10209534.html 四大作用域 为了在页面.请求.和用户之间传递和共享数据,JS ...

  8. jsp里面不能使用${pageContext.request.contextPath}解决方案

    问题:  在jsp中使用${pageContext.request.contextPath}获取相对路径,可是最后路径变为:http://localhost:8080/TMIS/$%7BpageCon ...

  9. 通过ActionContext获取request session application 以及ActionContext的简单解析

    ActionContext:action的上下文对象. 获取application : // 获取ActionContext对象 是action的上下文对象 ActionContext actionC ...

最新文章

  1. php goto call,Php中的goto用法
  2. POJ 1821 Fence ★(单调队列优化DP)
  3. Web应用开发技术(2)-html
  4. java批量导入数据到excel
  5. 《PHP综合开发环境》(NuSphere PhpED v5.6.5615 Win32)[压缩包]
  6. python解析pcap包已text格式输出_python分析pcap包
  7. 媒体查询Media Queries详解
  8. GDAL\OGR C#中文路径不支持的问题解决方法
  9. inDesign教程,如何使用 Pantone 颜色为黑白图像着色?
  10. vue视频文本编辑器html,Vue 轻量级富文本编辑器 Vue-Quill-Editor
  11. 最新小额借贷系统完整源码+附教程文档
  12. 漫步数理统计三十四——顺序统计量
  13. mybatis 插入insert对象
  14. TT语音借游戏社交“剑走偏锋”,能解“孤独经济”难题?
  15. Javascript-实现全局事件总线Event Bus/ Event Emitter
  16. java模拟超市商品库存管理平台
  17. Ayla CEO大卫.弗里德曼:你应该了解的五种物联网大数据!
  18. Axure RP 8.1 下载(附汉化+注册码)
  19. 论文翻译[Deep Residual Learning for Image Recognition]
  20. 即刻智能|MES生产制造管理系统助力企业实现“智慧工厂”

热门文章

  1. JASS代码翻译更新(第九篇)
  2. 建立IT投资效益分析模型
  3. Abaqus启动报错 FlexNet Licensing error:-7,96
  4. 逻辑思维类面试题汇集(1)
  5. 持续集成交付的流水作业
  6. 数字化转型过程中的六要素(5M1E):
  7. 硅光集成已成为未来数据中心高速光模块的首选解决方案
  8. C++程序设计基础,数组实验(题干信息:某班期末考试科目为数学(MT)、英语(EN)和物理(PH),有最多不超30人参加考试,具体问题在下面)
  9. 03 文法产生式的解析<1>
  10. java交通灯英文文献_java 交通灯管理系统源代码.doc