(案例)使用Cookie保存用户最后一次访问的时间
生活随笔
收集整理的這篇文章主要介紹了
(案例)使用Cookie保存用户最后一次访问的时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 需求
- 分析
- 示例代碼
需求
1.訪問一個Servlet,如果獲取不到上一次訪問的時間,則提示:您好,歡迎訪問。
2.如果可以獲取上一次訪問的時間,則提示:您好,歡迎回來,您上次訪問時間為:顯示時間字符串
分析
在服務器中的Servlet判斷是否有一個名為 lastTime 的 cookie
1.有
則響應數據:您好,歡迎回來,您上次訪問時間為:2018年6月10日11:50: 20
寫回 Cookie:lastTime=2018年6月10日11 :50:01
2.沒有
響應數據:您好,歡迎訪問
寫回 Cookie:lastTime=2018年6月10日11 :50:01
示例代碼
package priv.lwx.cs.example; /*** 通過Cookie保存用戶最后一次訪問的時間** @author liaowenxiong* @date 2022/1/14 16:39*/import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.net.URLDecoder; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date;@WebServlet("/last_time") public class SaveLastTimeByCookieServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");boolean flag = false;PrintWriter writer = response.getWriter();// 獲取所有的CookieCookie[] cookies = request.getCookies();// 遍歷Cookie數組,查找是否存在名為lasttime的Cookieif (cookies != null && cookies.length != 0) {for (Cookie cookie : cookies) {String name = cookie.getName();if ("lastTime".equals(name)) {// 存在名為lastTime的Cookie,則將變量flag的值設為trueflag = true;String value = cookie.getValue();// value是application/x-www-form-urlencoded字符串,需要解碼String decDate = URLDecoder.decode(value, "utf-8");writer.write("<h1>您好,歡迎回來,您上次訪問的時間:" + decDate + "</h1>");// 獲取系統當前時間Date date = new Date();// 創建日期格式對象SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// 格式化日期對象,返回一個日期字符串String strDate = sdf.format(date);System.out.println("URL編碼前:" + strDate);// URL編碼,因為Cookie無法存儲空格等特殊字符,所以只能將含有空格的字符串轉換成application/x-www-form-urlencoded字符串進行存儲String encDate = URLEncoder.encode(strDate, "utf-8");System.out.println("URL編碼后:" + encDate);// 用最新的系統時間替換名為lastTime的Cookie中舊的日期值cookie.setValue(encDate);// 將名為lastTime的Cookie添加到Response對象中response.addCookie(cookie);break;}}}// 沒有名為lastTime的Cookieif (cookies == null || cookies.length == 0 || flag == false) {// 獲取系統當前時間Date date = new Date();// 創建日期格式對象SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// 格式化日期對象,返回一個日期字符串String strDate = sdf.format(date);System.out.println("URL編碼前:" + strDate);// URL編碼,因為Cookie無法存儲空格等特殊字符,所以只能將含有空格的字符串轉換成application/x-www-form-urlencoded字符串進行存儲String encDate = URLEncoder.encode(strDate, "utf-8");System.out.println("URL編碼后:" + encDate);// 創建CookieCookie cookie = new Cookie("lastTime", encDate);// 設置Cookie的存活時間為1個月cookie.setMaxAge(30*24*60*60);response.addCookie(cookie);writer.write("<h1>您好,歡迎訪問!</h1>");}} }注意:Cookie無法保存空格等字符串,需要進行URL編碼進行保存,服務端取值時要URL解碼
總結
以上是生活随笔為你收集整理的(案例)使用Cookie保存用户最后一次访问的时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 水象星座有哪些(火象星座有哪些)
- 下一篇: 你真的会用Mac中的Finder吗?