保存作用域--尚硅谷(33)

1.保存作用域

原始情况下,保存作用域我们可以认为有四个:
page(页面级别,现在几乎不用)
request(一次请求响应范围)
session(一次会话范围)
application(整个应用程序范围)

1、request:一次请求响应范围


public class demo01Servlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //向request保存作用域中保存数据
        request.setAttribute("uname","lili");
       /*1.客户端重定向:在客户端重定向的情况下,客户端向demo01发出请求,
       demo01对客户端做出响应(进行重定向到demo02),然后客户端再demo02发出请求(地址栏中显示的是demo02)
       */ 
        //response.sendRedirect("demo02");
        /*2.服务器端转发:在服务器端转发的情况下,客户端向demo01发出请求,
        demo01对客户端将请求转发给demo02,然后demo02对客户端的请求进行响应(地址栏中显示的仍是demo01)
       */ 
        request.getRequestDispatcher("demo02").forward(request,response);
    }
}

public class demo02Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取request保存作用域保存的数据
        Object unameObj = request.getAttribute("uname");
        System.out.println("unameObj = "+unameObj);
    }
}

保存作用域--尚硅谷(33)_第1张图片

2、 session:一次会话范围有效

切换客户端就不能进行
客户端重定向和服务器端转发均可以,只要在一次会话范围内均可

@WebServlet("/demo03")
public class demo03Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.向request保存作用域保存数据
        request.getSession().setAttribute("uname","lili");
        //2、客户端重定向
        response.sendRedirect("demo04");
        //3、服务器端转发
        request.getRequestDispatcher("demo04").forward(request,response);
    }
}
@WebServlet("/demo04")
public class demo04Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取request保存作用域保存的数据
        Object unameObj = request.getSession().getAttribute("uname");
        System.out.println("unameObj = "+unameObj);
    }
}

保存作用域--尚硅谷(33)_第2张图片

3、 application:一次程序范围有效

切换客户端仍能进行

保存作用域--尚硅谷(33)_第3张图片

@WebServlet("/demo05")
public class demo05Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.向application保存作用域保存数据
        ServletContext application = request.getServletContext();//servletContext是上下文
        application.setAttribute("uname","lili");
        //2.客户端重定向
        response.sendRedirect("demo06");
        //服务器端转发
        //request.getRequestDispatcher("demo04").forward(request,response);
    }
}
@WebServlet("/demo06")
public class demo06Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext application = request.getServletContext();
        //获取application保存作用域保存的数据
        Object unameObj = application.getAttribute("uname");
        System.out.println("unameObj = "+unameObj);
    }
}

你可能感兴趣的:(Javaweb,tomcat,intellij-idea,java,java-ee)