Java Web會(huì)話技術(shù)Session的簡(jiǎn)單使用
Session技術(shù)是將信息保存在服務(wù)端,而客戶端需要接收、記錄和回送Session的ID,所以Session通常情況下是借助Cookie技術(shù)來(lái)傳遞ID給服務(wù)端的,服務(wù)端拿到session id之后查詢內(nèi)存中對(duì)應(yīng)的記錄。
一個(gè)客戶端對(duì)應(yīng)一個(gè)Session,而一個(gè)Session有多個(gè)Attribute,每一個(gè)Attribute有唯一的name。
編寫代碼證明提出的觀點(diǎn):
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(); PrintWriter writer = resp.getWriter(); // 給session綁定一個(gè)user對(duì)象 session.setAttribute('user', new User(1, 'kongsam')); List<String> users = new ArrayList<>(); users.add('kongsam'); users.add('xiaoming'); users.add('xiaohong'); // 給session綁定一個(gè)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); }}
兩個(gè)不同的瀏覽器就是兩個(gè)不同的客戶端,這兩個(gè)客戶端對(duì)應(yīng)不同的JSESSIONID。
Cookie的工作原理以及講解請(qǐng)見(jiàn)://www.jb51.net/article/212734.htm
Session如何工作在現(xiàn)實(shí)生活中,當(dāng)你去理發(fā)店理發(fā)時(shí),你可以選擇在前臺(tái)辦理一張會(huì)員卡,前臺(tái)工作人員將你的基本信息和之后的消費(fèi)信息等都存儲(chǔ)到店家電腦的硬盤上,在以后消費(fèi)的時(shí)候你僅憑一張會(huì)員卡就可以查詢到你所有的信息和消費(fèi)記錄。注意,這里的你是指客戶端,前臺(tái)(店家)指的是服務(wù)端。
SessionDemo01是用來(lái)創(chuàng)建一個(gè)假的數(shù)據(jù)庫(kù),并且把這個(gè)數(shù)據(jù)庫(kù)存放到Session中進(jìn)行保管。
@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(); // 創(chuàng)建一個(gè)假數(shù)據(jù)庫(kù) 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ù)庫(kù)的數(shù)據(jù)存放到Session中 session.setAttribute('vipUsers', vipUsers);}
然后SessionDemo02用于訪問(wèn)Session里vipsUsers數(shù)據(jù)庫(kù),如果用戶沒(méi)有辦理或者不存在該用戶則為其注冊(cè)一個(gè)新VIP。
@Overrideprotected 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ù)庫(kù) Map<String, VipUser> maps = (Map<String, VipUser>) session.getAttribute('vipUsers'); // 判斷數(shù)據(jù)庫(kù)中是否有和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('您還沒(méi)有辦理會(huì)員卡,前臺(tái)正在為您辦理中...,請(qǐng)刷新頁(yè)面。');maps.put(username, new VipUser(1, username, '123', 50));session.setAttribute('vipUsers', maps); }}
來(lái)看看效果吧!
以上就是Java Web會(huì)話技術(shù)Session的簡(jiǎn)單使用的詳細(xì)內(nèi)容,更多關(guān)于Java Session的使用的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 前端從瀏覽器的渲染到性能優(yōu)化2. 無(wú)線標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)3. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)4. 讀大數(shù)據(jù)量的XML文件的讀取問(wèn)題5. 解析原生JS getComputedStyle6. PHP循環(huán)與分支知識(shí)點(diǎn)梳理7. css代碼優(yōu)化的12個(gè)技巧8. 利用CSS3新特性創(chuàng)建透明邊框三角9. ASP實(shí)現(xiàn)加法驗(yàn)證碼10. ASP基礎(chǔ)入門第三篇(ASP腳本基礎(chǔ))
