①请求域(超常用)
请求域时间最短,范围也是最小的.一次请求内有效,请求完成.请求域中的数据就失效了
在请求转发的场景下,我们可以借助HttpServletRequest对象内部给我们提供的存储空间,帮助我们携带数据,把数据发送给转发的目标资源。
请求域:HttpServletRequest对象内部给我们提供的存储空间
通俗来说就是把服务器中的数据传递到网页
package com.sun.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/RegionServlet")
public class RegionServlet extends ViewBaseServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("username", "大聪明");
req.setAttribute("password", "12334");
req.setAttribute("phone", "12232298");
super.processTemplate("region",req,resp);
}
}
下面演示了两种取值方法,在表单元素里面,我们使用value,而不使用text
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>域测所</title>
</head>
<body>
<h1>域测试获取值</h1>
用户名:<input type="text" th:value="${username}"><br>
密码:<input type="text" th:value="${password}"><br>
手机:<input type="text" th:value="${phone}"><br>
<div>[[${username}]]</div>
<div>[[${password}]]</div>
<div>[[${phone}]]</div>
<h1 th:text="${username}"></h1>
<h1 th:text="${password}"></h1>
<h1 th:text="${phone}"></h1>
</body>
</html>
②会话域(常用)
设置session的超时时间,session作用时间长,范围广。
session会话域,默认可以保存30分钟数据有效果,但是前提是不要关闭浏览器,如果关闭浏览器再一次访问,就访问不到session中的数据了,原因是跟cookie有关。会话域的取值需要在本页面上取,才能在其他页面上取到,这是因为会话域的数据是存储在服务器端的,通过会话ID来进行关联。当用户在某个页面上进行操作时,服务器会根据会话ID获取相应的会话数据。因此,需要在当前页面上获取会话数据,然后在其他页面上传递会话ID,才能在其他页面上获取到相应的会话数据。
<session-config>
<!--session超时,时间设置,默认时间是30分钟-->
<session-timeout>30</session-timeout>
</session-config>
HttpSession session = req.getSession();
session.setAttribute("username", "小明");
session.setAttribute("password", "13202");
session.setAttribute("phone", "12087083");
<h1>会话域测试取值</h1>
用户名:<input type="text" th:value="${session.username}"><br>
密码:<input type="text" th:value="${session.password}"><br>
手机:<input type="text" th:value="${session.phone}"><br>
<div>[[${session.username}]]</div>
<div>[[${session.password}]]</div>
<div>[[${session.phone}]]</div>
<h1 th:text="${session.username}"></h1>
<h1 th:text="${session.password}"></h1>
<h1 th:text="${session.phone}"></h1>
会话域案例
当我们登录的时候,可能需要如图的用户名,这样的用户名我们怎么获取呢?当然是通过会话域了,因为这样的页面有很多个,我们可以使用sessoion在任意页面去访问这个用户名。
③应用域(使用较少)
页面取值时,使用${application.xxx},这个application就是我们servlet中的servletContext对象,作用周期很长,随着web项目启动而创建,随着web项目关闭而消失。
<h1>会话域测试取值</h1>
用户名:<input type="text" th:value="${application.username}"><br>
密码:<input type="text" th:value="${application.password}"><br>
手机:<input type="text" th:value="${application.phone}"><br>
<div>[[${application.username}]]</div>
<div>[[${application.password}]]</div>
<div>[[${application.phone}]]</div>
<h1 th:text="${application.username}"></h1>
<h1 th:text="${application.password}"></h1>
<h1 th:text="${application.phone}"></h1>
//上下文对象叫做应用域也叫做全局作用域
ServletContext servletContext = getServletContext();
servletContext.setAttribute("username", "小米");
servletContext.setAttribute("password", "12222");
servletContext.setAttribute("phone", "7722922");
super.processTemplate("region", req, resp);
会话域和请求域的区别
会话域和请求域是Web开发中常用的两种数据存储方式
1. 会话域(Session):会话域是服务器端用于存储用户会话数据的一种机制。在用户第一次访问服务器时,服务器会为该用户创建一个唯一的会话ID,并将该ID发送给客户端保存(通常以Cookie的形式)。之后,客户端每次请求都会携带该会话ID,服务器根据该ID来识别用户,并将相应的会话数据与之关联。会话域的数据可以在多个请求之间共享,直到会话结束或超时。
应用场景:适用于需要在不同页面或请求之间共享数据的场景,如用户登录状态、购物车信息等。
2. 请求域(Request):请求域是在一次HTTP请求中有效的数据存储方式,它只在当前请求的处理过程中有效,请求结束后即被销毁。请求域的数据无法在不同请求之间共享,仅在当前请求的处理过程中可用。
应用场景:适用于需要在同一次请求的不同处理过程中传递数据的场景,如表单提交、请求重定向等。