ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javaweb学习15:Session(重点)

2022-03-27 14:32:05  阅读:159  来源: 互联网

标签:15 javaweb ServletException Session resp req session void


javaweb学习15:Session(重点)

  • Session:

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

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

       

  • Session和Cookie的区别:

    • Cookie:把用户的数据写给用户的浏览器;浏览器保存;(可以保存多个)

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

    • Session对象由服务器创建;

       

  • 使用场景:

    • 保存登录用户的信息;

    • 保存购物车的信息;

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

 

  • 代码案例:创建Session信息

    /**
    * Session
    */
    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.setAttribute("name",new Person("张三",1));
           //获取session的ID
           String id=session.getId();
           //判断session是不是新创建的
           if(session.isNew()){
               resp.getWriter().write("session创建成功,id为:"+id);
          }else{
               resp.getWriter().write("session已经在服务器中存在了");
          }

           //Session创建的时候做了什么事情:
           /*Cookie cookie = new Cookie("JSESSIOONID",id);
           resp.addCookie(cookie);*/


      }

       @Override
       protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           doGet(req, resp);
      }
    }



     

  • 代码案例:获取Session信息;


    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();
           Person person= (Person) session.getAttribute("name");
           resp.getWriter().write(person.toString());
           System.out.println(person.toString());


      }

       @Override
       protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           doGet(req, resp);
      }

    }

     

  • 代码案例:注销Session


    public class SessionServlet03 extends HttpServlet {

       @Override
       protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

           //得到session
           HttpSession session = req.getSession();
           session.removeAttribute("name");//取消session
           session.invalidate();//手动注销session

      }

       @Override
       protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           doGet(req, resp);
      }
    }

     

  • 代码案例:会话自动过期:以分钟为单位

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
            version="4.0"
            metadata-complete="true">
       
       
    <!--设置Session默认注销时间-->
     <session-config>
    <!--1分钟后,session自动失效;以分钟为单位-->
       <session-timeout>1</session-timeout>
     </session-config>
       
       
    </web-app>

     

 

 

 

 

 

 

 

标签:15,javaweb,ServletException,Session,resp,req,session,void
来源: https://www.cnblogs.com/xiangcai0522/p/16062747.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有