1 使用Cookie實現(xiàn)顯示用戶的上次訪問時間
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 頁面輸出 response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); // 獲取字符輸出流對象 PRintWriter out = response.getWriter(); // 獲取Cookie數(shù)組對象 Cookie [] cookies = request.getCookies(); // 定義一個時間的字符串變量 String date = null; // 定義一個變量存儲系統(tǒng)當(dāng)前日期 Date current_date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // 判斷是否是第一次登陸 if(cookies != null){ // 直接循環(huán) for(Cookie cookie : cookies){ // 獲取Cookie if("lasttime".equals(cookie.getName())){ // 獲取上次訪問的時間 date = cookie.getValue(); break; }else{ // 獲取系統(tǒng)時間 date = format.format(current_date); } } }else{ // 獲取系統(tǒng)時間 date = format.format(current_date); } // 顯示時間 out.println(date); // 將這次訪問的時間寫入Cookie Cookie new_cookie = new Cookie("lasttime",format.format(new Date())); new_cookie.setMaxAge(5*60); new_cookie.setPath("/day08/showtime"); // 發(fā)送 response.addCookie(new_cookie); }Cookie細(xì)節(jié)在上面使用Cookie技術(shù)存儲會話信息的時候發(fā)現(xiàn)Cookie存儲的數(shù)據(jù)有限,而且每次需要客戶端瀏覽器攜帶數(shù)據(jù),導(dǎo)致網(wǎng)絡(luò)的負(fù)載過大。因此如果需要存儲相對大量的數(shù)據(jù),那么可以直接將數(shù)據(jù)存儲在服務(wù)器端,這樣可以提高數(shù)據(jù)的訪問速度。
HttpSession技術(shù)就是將會話的數(shù)據(jù)存儲在服務(wù)器端,便于開發(fā)者直接進(jìn)行訪問。
1 HttpSession接口
該接口主要定義了一種區(qū)分不同用戶并通過request對象獲取該對象的實例存儲與用戶相關(guān)的信息的方式。
該接口的對象是tomcat服務(wù)器幫助開發(fā)者創(chuàng)建出來的,當(dāng)開發(fā)者調(diào)用request.getSession()方法獲取該接口的對象。
2 常用API
獲取HttpSession對象
HttpSession getSession() ? 如果有直接返回,如果沒有直接創(chuàng)建并返回HttpSession getSession(boolean create) ? true同上,false有返回,沒有null
操作數(shù)據(jù)
void setAttribute(String name, Object value) ? 設(shè)置指定名的屬性值 Object getAttribute(String name) ? 獲取指定名的屬性值Enumeration getAttributeNames() ? 獲取所有屬性名的集合迭代器void removeAttribute(String name) ? 刪除指定名的屬性
額外的方法
String getId() ? 獲取Session的ID值boolean isNew() ? 判斷Session是否是新的long getCreationTime() ? 獲取Session創(chuàng)建時間的longlong getLastaccessedTime() ? 獲取最后一次訪問Session的時間void invalidate() ? 指定Session無效
3 HttpSession的讀和寫
1. 寫Session數(shù)據(jù)
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 獲取session對象 HttpSession session = request.getSession(); // 放置數(shù)據(jù) session.setAttribute("name", "jack"); }發(fā)現(xiàn)響應(yīng)信息中以Set-Cookie: JSESSIONID=8AA06CD311EC6A38805C73C93B8FCE4F; Path=/day08響應(yīng)頭數(shù)據(jù)將HttpSession對象的ID值以Cookie的方式發(fā)送給瀏覽器進(jìn)行存儲。
2. 讀Session數(shù)據(jù)
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 獲取Session對象 HttpSession session = request.getSession(); // 取Session數(shù)據(jù) String name = (String) session.getAttribute("name"); // 輸出信息 System.out.println(name);}請求消息中使用Cookie: JSESSIONID=8AA06CD311EC6A38805C73C93B8FCE4F攜帶了HttpSession對象的ID值,那么服務(wù)器根據(jù)該值找到對應(yīng)的HttpSession對象獲取其中的值。
總結(jié):
HttpSession技術(shù)底層需要借助Cookie存儲服務(wù)器端為每一個用戶創(chuàng)建的HttpSession對象的ID值,便于后面拿到該值獲取服務(wù)器指定對象中值。
3 重新審視以上代碼
可以將獲取數(shù)據(jù)的servlet中的獲取sessiond的代碼修改如下
HttpSession session = request.getSession(false);
現(xiàn)在實際的瀏覽器在啟動多個同一個瀏覽器窗口,那么自動使用同一個Session對象。一旦Session的有效時間超過了半個小時那么Session自動銷毀。
4 其他的常用方法
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 獲取session對象 HttpSession session = request.getSession(); // 調(diào)用常用的方法 System.out.println("getId(): "+session.getId()); System.out.println("getLastAccessedTime(): "+session.getLastAccessedTime()); System.out.println("isNew(): "+session.isNew()); System.out.println("getCreationTime(): "+session.getCreationTime());}運行結(jié)果
getId(): F8A7BC23A0B403CE30A69F8B5F903D6AgetLastAccessedTime(): 1358385915203isNew(): truegetCreationTime(): 1358385915203
如果在以上的代碼中使用了 session.invalidate();后繼續(xù)往session中添加數(shù)據(jù),那么服務(wù)器報錯
java.lang.IllegalStateException: setAttribute: Session already invalidated
總結(jié):
登陸功能分析à 1. 獲取用戶數(shù)據(jù) 2. 校驗數(shù)據(jù) 3. 成功,在Session中存儲用戶登陸的標(biāo)識
注銷功能分析à 1. 清除Session中的用戶標(biāo)識信息 2. 重定向到登陸頁面
新聞熱點
疑難解答