概念:服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器的对象中。HttpSession

快速入门:

  获取HttpSession对象:

    HttpSession session = request.getSession();

    使用HttpSession对象:

      Object  getAttribute(String name)

      void setAttribute(String name,Object value)

      void   removeAttribute(String name)

 

 

@WebServlet("/SessionDemo1")
public class SessionDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();

        session.setAttribute("msg","hello session");


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

 

@WebServlet("/SessionDemo2")
public class SessionDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();

        Object msg = session.getAttribute("msg");
        System.out.println(msg);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

 

 

 

 

 

 

 

 

 

 

Session原理”

  Session的实现是依赖Cookie的

 

 

 

posted on 2022-08-15 09:55  淤泥不染  阅读(9)  评论(0编辑  收藏  举报