zoukankan      html  css  js  c++  java
  • JavaWeb HttpSession

    /**
     * 使用session共享数据
     */
    public class SessionDemo1 extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //使用session共享数据
            //1.获取session
            HttpSession session = req.getSession();
            //2.存储数据
            session.setAttribute("msg0","HelloSession");
        }
    }
    
    /**
     * 获取session共享的数据
     */
    public class SessionDemo2 extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //使用session获取数据
            //1.获取session
            HttpSession session = req.getSession();
            //2.获取数据
            Object msg = session.getAttribute("msg0");
            System.out.println(msg);
        }
    }
    
    /**
     * 使用Cookie将Session保存到浏览器,且关闭浏览器再打开后不会消失
     */
    public class SessionDemo3 extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //1.获取Session
            HttpSession session = req.getSession();
            System.out.println(session);
    
            //期望客户端关闭后,session也能相同
            Cookie c = new Cookie("JSESSIONID",session.getId());
            resp.addCookie(c);
        }
    
    }
    

      

  • 相关阅读:
    常见错误集锦
    auto 迭代器的使用
    案例:带有动画的返回顶部
    案例:toDoList
    jQuery事件
    案例:发布微博功能
    案例:购物车功能模块
    jQuery常用的API
    案例:王者荣耀手风琴效果
    案例:jQuery实现tab栏切换功能
  • 原文地址:https://www.cnblogs.com/MineLSG/p/13922264.html
Copyright © 2011-2022 走看看