App下載

解析JavaWeb會話技術Session的使用

諸葛亮晶晶 2021-08-16 15:15:08 瀏覽數(shù) (1874)
反饋

什么是Session

Session技術是將信息保存在服務端,而客戶端需要接收、記錄和回送Session的ID,所以Session通常情況下是借助Cookie技術來傳遞ID給服務端的,服務端拿到session id之后查詢內存中對應的記錄。

一個客戶端對應一個Session,而一個Session有多個Attribute,每一個Attribute有唯一的name。

編寫代碼證明提出的觀點:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession();
    PrintWriter writer = resp.getWriter();
    // 給session綁定一個user對象
    session.setAttribute("user", new User(1, "kongsam"));
    List<String> users = new ArrayList<>();
    users.add("kongsam");
    users.add("xiaoming");
    users.add("xiaohong");
    // 給session綁定一個list數(shù)組
    session.setAttribute("list", users);
    // 最后打印輸出
    writer.println("JSESSIONID = " + session.getId());
    writer.println("object => user = " + session.getAttribute("user").toString());
    for (String user : users) {
        writer.println("list => user = " + user);
    }
}

兩個不同的瀏覽器就是兩個不同的客戶端,這兩個客戶端對應不同的JSESSIONID。

Cookie的工作原理以及講解請見://www.jb51.net/article/212734.htm

Session如何工作

在現(xiàn)實生活中,當你去理發(fā)店理發(fā)時,你可以選擇在前臺辦理一張會員卡,前臺工作人員將你的基本信息和之后的消費信息等都存儲到店家電腦的硬盤上,在以后消費的時候你僅憑一張會員卡就可以查詢到你所有的信息和消費記錄。注意,這里的你是指客戶端,前臺(店家)指的是服務端。

結合代碼理解Session

SessionDemo01是用來創(chuàng)建一個假的數(shù)據(jù)庫,并且把這個數(shù)據(jù)庫存放到Session中進行保管。

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession();
    // 創(chuàng)建一個假數(shù)據(jù)庫
    Map<String, VipUser> vipUsers = new HashMap<>();
    vipUsers.put("kongsam", new VipUser(1, "kongsam", "123", 50));
    vipUsers.put("xiaoming", new VipUser(2, "xiaoming", "123", 100));
    vipUsers.put("xiaohong", new VipUser(3, "xiaohong", "123", 200));
    // 將假數(shù)據(jù)庫的數(shù)據(jù)存放到Session中
    session.setAttribute("vipUsers", vipUsers);
}

然后SessionDemo02用于訪問Session里vipsUsers數(shù)據(jù)庫,如果用戶沒有辦理或者不存在該用戶則為其注冊一個新VIP。

@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");
    HttpSession session = req.getSession();
    // 獲取username
    String username = req.getParameter("username");
    // 從Session中取出數(shù)據(jù)庫
    Map<String, VipUser> maps = (Map<String, VipUser>) session.getAttribute("vipUsers");
    // 判斷數(shù)據(jù)庫中是否有和username匹配的用戶
    if (maps.get(username) != null && maps.get(username).getUsername().equals(username)) {
        resp.getWriter().println(maps.get(username).getUsername() + "您好,您目前的積分是: " + maps.get(username).getPoints());
    } else {
        resp.getWriter().println("您還沒有辦理會員卡,前臺正在為您辦理中...,請刷新頁面。");
        maps.put(username, new VipUser(1, username, "123", 50));
        session.setAttribute("vipUsers", maps);
    }
}

來看看效果吧!

以上就是Java Web會話技術Session的簡單使用的詳細內容,更多關于Java Session的使用的資料,請關注W3Cschool其它相關文章!


0 人點贊