ServletContext域对象是一个全局性的域对象,它的生命周期与Web应用程序的生命周期一致,即当Web应用程序被加载时,与之对应的ServletContext对象会被创建;只要Web应用程序还存在,这个域对象就会一直存在;当Web应用程序被卸载或者服务器关闭时,ServletContext对象也会随之消亡。ServletContext域对象的主要作用是提供一种机制,允许开发者在其内部设置和获取数据,这些数据可以在整个Web应用程序的范围内共享。它提供了一些API,如setAttribute(String key, Object value)
、getAttribute(String key)
以及removeAttribute(String key)
等方法来实现这些功能。
Request域对象也是一个域对象,它主要用于处理一次HTTP请求。它的生命周期与本次请求相关,一旦请求结束,Request对象也就不再存在了。Request域对象的主要用途是在处理请求时保存和传递数据。它同样提供了一些API,如setAttribute(String name, Object value)
,用于在请求间共享数据,以及在请求结束后清除这些数据。
Session域对象则专门用于处理一次HTTP会话。它会随着会话的建立而创建,并在会话结束时销毁。Session域对象的主要目的是保存用户的状态信息,以便在整个会话期间为不同的请求提供相同的用户状态。它也提供了一些API,如setAttribute(String name, Object value)
,用于在会话间共享数据,以及removeAttribute(String name)
用于清除会话结束时的数据
2.1.request共享数据
控制类
@RequestMapping("servletAPI")
public String servletApi(HttpServletRequest request)
{
request.setAttribute("testscope","hello,world!");
return "scope";
}
界面
对于request,可以直接使用变量名来访问
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p th:text="${testscope}">p>
body>
html>
返回类型必须是ModelAndView
@RequestMapping("ModelAndView")
public ModelAndView testModelAndView()
{
ModelAndView modelAndView =new ModelAndView();
//向request域共享数据
modelAndView.addObject("testscope","hello,ModelAndView!");
//设置视图名称 转发到scope.html页面
modelAndView.setViewName("scope");
return modelAndView;
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p th:text="${testscope}">p>
body>
html>
@RequestMapping("model")
public String model(Model model)
{
model.addAttribute("testscope","hello,Model!");
return "scope";
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p th:text="${testscope}">p>
body>
html>
@RequestMapping("map")
public String map(Map<String, Object> map)
{
map.put("testscope","hello,Map!");
return "scope";
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p th:text="${testscope}">p>
body>
html>
@RequestMapping("modelMap")
public String modelMap(ModelMap map)
{
map.addAttribute("testscope","hello,ModelMap!");
return "scope";
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p th:text="${testscope}">p>
body>
html>
@RequestMapping("session")
public String testsession(HttpSession session)
{
session.setAttribute("testscope","hello,Session!");
return "scope";
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p th:text="${testscope}">p>
<p th:text="${session.testscope}">p>
body>
html>
@RequestMapping("application")
public String testapplication(HttpSession session)
{
ServletContext application = session.getServletContext();
application.setAttribute("testscope","hello,application!");
return "scope";
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p th:text="${testscope}">p>
<p th:text="${application.testscope}">p>
body>
html>