在 JavaWeb 中,共享域指的是在 Servlet 中存储数据,以便在同一 Web 应用程序的多个组件中进行共享和访问。常见的共享域有四种:ServletContext
、HttpSession
、HttpServletRequest
、PageContext
。
ServletContext
共享域:ServletContext
对象可以在整个 Web 应用程序中共享数据,是最大的共享域。一般可以用于保存整个 Web 应用程序的全局配置信息,以及所有用户都共享的数据。在 ServletContext
中保存的数据是线程安全的。
HttpSession
共享域:HttpSession
对象可以在同一用户发出的多个请求之间共享数据,但只能在同一个会话中使用。比如,可以将用户登录状态保存在 HttpSession
中,让用户在多个页面间保持登录状态。
HttpServletRequest
共享域:HttpServletRequest
对象可以在同一个请求的多个处理器方法之间共享数据。比如,可以将请求的参数和属性存储在 HttpServletRequest
中,让处理器方法之间可以访问这些数据。
PageContext
共享域:PageContext
对象是在 JSP 页面Servlet 创建时自动创建的。它可以在 JSP 的各个作用域中共享数据,包括pageScope
、requestScope
、sessionScope
、applicationScope
等作用域。
共享域的作用是提供了方便实用的方式在同一 Web 应用程序的多个组件之间传递数据,并且可以将数据保存在不同的共享域中,根据需要进行选择和使用。
本次场景演示使用Thymeleaf服务器渲染技术。
向Request域中共享数据
使用HttpServletRequest向域中共享数据
| @GetMapping("/testServletScope") |
| public String testServlet(HttpServletRequest request) { |
| request.setAttribute("testRequestScope", "hello,servlet"); |
| return "success"; |
| } |
使用ModelAndView向域中共享数据
| @GetMapping("/testModelAndView") |
| public ModelAndView testModelAndView() { |
| ModelAndView modelAndView = new ModelAndView(); |
| modelAndView.addObject("testRequestScope", "hello,ModelAndView"); |
| modelAndView.setViewName("success"); |
| return modelAndView; |
| } |
使用Model向域中共享数据
| @GetMapping("/testModel") |
| public String testModel(Model model) { |
| model.addAttribute("testRequestScope", "hello,Model"); |
| return "success"; |
| } |
使用Map集合向域中共享数据
| @GetMapping("/testMap") |
| public String testMap(Map<String, Object> map) { |
| map.put("testRequestScope","hello,Map"); |
| return "success"; |
| } |
使用ModelMap向域中共享数据
| @GetMapping("/testModelMap") |
| public String testModelMap(ModelMap modelMap) { |
| modelMap.addAttribute("testRequestScope","hello,ModelMap"); |
| return "success"; |
| } |
向session域中共享数据
| @GetMapping("/testSession") |
| public String testSession(HttpSession session) { |
| session.setAttribute("testSessionScope","hello,session"); |
| return "success"; |
| } |
向Application域中共享数据
| |
| @GetMapping("/testApplication") |
| public String testApplication(HttpSession session) { |
| ServletContext application = session.getServletContext(); |
| application.setAttribute("testApplicationScope","hello,Application"); |
| return "success"; |
| } |
| |
| |
| |
| @Autowired |
| private ServletContext servletContext; |
| |
| @GetMapping("/testApplication2") |
| public String testApplication2(HttpSession session) { |
| ServletContext application = session.getServletContext(); |
| application.setAttribute("testApplicationScope","Hello,ApplicationContext"); |
| return "success"; |
| } |
测试
创建testScope.html
| <!DOCTYPE html> |
| <html lang="en" xmlns:th="http://www.thymeleaf.org"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>测试域对象</title> |
| </head> |
| <body> |
| <a th:href="@{/testServletScope}">测试通过Servlet向域中共享数据</a><br/> |
| <a th:href="@{/testModelAndView}">测试通过ModelAndView向域中共享数据</a><br/> |
| <a th:href="@{/testModel}">测试通过Model向域中共享数据</a><br/> |
| <a th:href="@{/testMap}">测试通过Map集合向域中共享数据</a><br/> |
| <a th:href="@{/testModelMap}">测试通过ModelMap向域中共享数据</a><br/> |
| <a th:href="@{/testSession}">测试向Session域中共享数据</a><br/> |
| <a th:href="@{/testApplication}">测试向Application域中共享数据</a><br/> |
| </body> |
| </html> |
创建success.html
| <!DOCTYPE html> |
| <html lang="en" xmlns:th="http://www.thymeleaf.org"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Title</title> |
| </head> |
| <body> |
| <h2>SUCCESS...</h2> |
| <p th:text="${testRequestScope}"></p> |
| <p th:text="${session.testSessionScope}"></p> |
| <p th:text="${application.testApplication}"></p> |
| </body> |
| </html> |
请求页面跳转
| @GetMapping("/testScope") |
| public String testScope() { |
| return "testScope"; |
| } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性