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