html文件: <!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> Request:<span th:text="${#httpServletRequest.getAttribute('red')}"></span><br/> Session:<span th:text="${session.sess}"></span><br/> Application:<span th:text="${application.app}"></span><br/> </body> </html>
controller 文件:
@RequestMapping("show5") public String showInfo5(HttpServletRequest request,Model model){ request.setAttribute("req","HttpServletRequest"); request.getSession().setAttribute("sess","HttpSession"); request.getSession().getServletContext().setAttribute("app","Application"); return "index5"; }
预备知识点:1、request.getSession()可以帮你得到HttpSession类型的对象,通常称之为session对象,session对象的作用域为一次会话,通常浏览器不关闭,保存的值就不会消失,当然也会出现session超时。服务器里面可以设置session的超时时间,web.xml中有一个session time out的地方,tomcat默认为30分钟。
2、(1)servletContext就是代表web应用的一个servlet类。也就是说一个web应用只有一个servletContext对象。
(2)servletContext是用来管理web应用下的资源的。
(3)servletContext是可以被所有访问其代表web应用的用户共享的。
(4)servletContext是在服务器端被创建和销毁的。