1. 程式人生 > 其它 >javaweb學習15:Session(重點)

javaweb學習15:Session(重點)

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>