狂神说 javaweb 17集:Seesion详解

17.Seesion详解

7.4、Session(重点)

在这里插入图片描述

 

什么是Session:

  • 服务器会给每一个用户(浏览器),创建一个Session对象;

  • 一个Session独占一个浏览器,只要浏览器页面没有关闭,这个Session就存在;

  • 用户登录之后,整个网站他都可以访问的!→保存用户的信息,保存购物车的信息。。。

 

Session跟Cookie的区别:

  • Cookie是把用户的数据写给用户的浏览器,浏览器保存

  • session 把用户的数据写到用户独占Session 中,服务器端保存(保存重要的信息,减少服务器资源的浪费)

  • Session对象由服务器对象创建;

 

使用场景:

  • 保存一个登录用户的信息;

  • 购物车信息;

  • 在整个网站中经常会使用的数据,我们将他保存在Session中;

 

使用Session:

SessionDemo01

 public class SessionDemo01 extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         //解决乱码问题
         req.setCharacterEncoding( "utf-8" );
         resp.setCharacterEncoding( "utf-8" );
         resp.setContentType( "text/html;charset=utf-8");
 
         //得到Session
         HttpSession session = req.getSession();
 
         //给Session中存东西
         session.setAttribute( "name",new Person("test",31) );
 
         //获取Session的ID
         String sessionId = session.getId();
 
         //判断是不是新创建的
         if(session.isNew()){
             resp.getWriter().write("Session创建成功,ID:"+sessionId );
        }else {
             resp.getWriter().write("Session已存在,ID:"+sessionId );
        }
 
         //Session创建的时候做了什么事情
         //Cookie cookie = new Cookie("JSESSIONID",sessionId);
         //resp.addCookie( cookie );
 
    }
 
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         doGet( req, resp );
    }
 }

得到Session

SessionDemo02

 public class SessionDemo02 extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         //解决乱码问题
         req.setCharacterEncoding( "utf-8" );
         resp.setCharacterEncoding( "utf-8" );
         resp.setContentType( "text/html;charset=utf-8");
 
         //得到Session
         HttpSession session = req.getSession();
 
         //给Session中存东西
         Object person = (Object) session.getAttribute( "name" );
 
         System.out.println(person.toString());
         resp.getWriter().write( person.toString() );
    }
 
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         doGet( req, resp );
    }
 }

回话自动过期

SessionDemo03

 public class SessionDemo03 extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         HttpSession session = req.getSession();
         session.removeAttribute( "name" );
         session.invalidate();
 
    }
 
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         doGet( req, resp );
    }
 }

xml

 <!--设置Session默认的失效时间-->
 <session-config>
     <!--15分钟后Session自动失效,以分钟为单位-->
     <session-timeout>15</session-timeout>
 </session-config>

Cookie的场合

在这里插入图片描述

Session的场合

在这里插入图片描述

 

自己思考:Cookie跟 Session跟 Contexttype的区别

 
posted @ 2022-05-22 22:45  坚持做  阅读(138)  评论(0)    收藏  举报