---
# System prepended metadata

title: 保存作用域
tags: [Java Web-Servlet]

---

# 保存作用域
###### tags: `Java Web-Servlet`

## request：一次請求響應範圍
![](https://i.imgur.com/mFtCLGd.png)

demo01使用sendRedirect(客戶端重定向)，響應1回到客戶端，請客戶端再發請求2到demo02
由於不是同一次響應，因此request(一次請求響應範圍)無法取得資料

![](https://i.imgur.com/1HTRFEf.png)
======================
![](https://i.imgur.com/0KDwenu.png)

demo01使用getRequestDispatcher(服務器端轉發)，demo01直接在服務器內部轉發給demo02，因此回傳給客戶端的還是響應1，因此能取得數據
![](https://i.imgur.com/c5c75TL.png)

```java=
//演示request保存作用域(demo01和demo02)
@WebServlet("/demo01")
public class Demo01Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.向request保存作用域保存數據
        req.setAttribute("uname","lili");
        //2.客戶端重定向
//        resp.sendRedirect("demo02");
        //3.服務器端轉發
        req.getRequestDispatcher("demo02").forward(req,resp);
    }
}
```
```java=
@WebServlet("/demo02")
public class Demo02Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.獲取request保存作用域保存的數據，key為uname
        Object unameObj = req.getAttribute("uname");
        System.out.println("unameObj = " + unameObj);
    }
}

```

## Session：一次會話範圍有效


只要一次會話中，不管是sendRedirect還是getRequestDispatcher，都能取得保存域中的資料
![](https://i.imgur.com/ZESIQn5.png)

```java=
//演示session保存作用域(demo03和demo04)
@WebServlet("/demo03")
public class Demo03Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.向session保存作用域保存數據
        req.getSession().setAttribute("uname","lili");
        //2.客戶端重定向
        resp.sendRedirect("demo04");
        //3.服務器端轉發
//        req.getRequestDispatcher("demo04").forward(req,resp);
    }
}
```
```java=
@WebServlet("/demo04")
public class Demo04Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.獲取request保存作用域保存的數據，key為uname
        Object unameObj = req.getSession().getAttribute("uname");
        System.out.println("unameObj = " + unameObj);
    }
}

```
![](https://i.imgur.com/hCde9fB.png)
客戶端二因為已經是不同會話，因此無法取得客戶端一保存在保存域中的資料

=============
## application：一次應用程序範圍有效
![](https://i.imgur.com/7BC9y69.png)
當前應用程序執行下，所有客戶端都能取得保存作用域的資料，直到應用程序關閉
![](https://i.imgur.com/eMMboIm.png)
```java=
//演示application保存作用域(demo05和demo06)
@WebServlet("/demo05")
public class Demo05Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.向request保存作用域保存數據
        //ServletContext：Servlet上下文，tomcat啟動後，直到關閉前都能讀取到保存域的數據
        ServletContext application = req.getServletContext();
        application.setAttribute("uname","lili");
        //2.客戶端重定向
        resp.sendRedirect("demo06");
        //3.服務器端轉發
//        req.getRequestDispatcher("demo04").forward(req,resp);
    }
}

```

```java=
@WebServlet("/demo06")
public class Demo06Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.獲取application保存作用域保存的數據，key為uname
        ServletContext application = req.getServletContext();
        Object unameObj = application.getAttribute("uname");
        System.out.println("unameObj = " + unameObj);
    }
}

```