一、保存作用域
- page:页面级别,基本不用
- request:一次请求响应范围
- session:一次会话范围
- application:整个应用程序范围
1.保存作用域
1.page
2.request
客户端重定向模式(两次请求响应),请求1设置的uname,请求2获取不到
服务器端转发(一次请求响应),请求1设置的uname,可以获取的到
@WebServlet("/demo01")
public class Demo01Servlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.向request保存作用域,保存数据
request.setAttribute("uname","lina");
//2.客户端重定向
response.sendRedirect("demo02");
//3.服务器端转发
request.getRequestDispatcher("demo02");
}
}
3.session
一次会话范围都有效
@WebServlet("/demo03")
public class Demo03Servlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.向session保存作用域,保存数据
request.getSession().setAttribute("uname","lina");
//2.客户端重定向
response.sendRedirect("demo02");
//3.服务器端转发
request.getRequestDispatcher("demo02");
}
}
4.application
@WebServlet("/demo04")
public class Demo04Servlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.向application保存作用域保存数据
//ServletContext Servlet上下文
ServletContext application = request.getServletContext();
application.setAttribute("uname","lina");
}
}