Case: Remember the last access time
1. Requirements:
1. Access a Servlet, if it is the first visit, the prompt: Hello, welcome to your first visit.
2. If it is not the first visit, the prompt: Welcome back, your last visit time: displays the time string
2. Analysis:
1. Cookie can be accomplished using
2. Servlet in the server to determine whether there is a file called lastTime of the cookie
1 are: not the first visit to
1. response data: Welcome back, your last visit time: June 10, 2018 11:50:20
2. write back Cookie: lastTime = 2018 Nian 6 Yue 10 11:50:01
2. no: is the first visit to
1. The response data: Hello, welcome to your first visit
2. write back Cookie: lastTime = 2018 Nian 6 Yue 10, 11:50:00
important point:
Must be set before the encoding stream 1. Get PrintWriter
response.setContentType("text/html;charset=utf-8");
2. cookie can not contain special characters, such as spaces, so to encode and decode
1 package cn.itcast.web.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.Cookie; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.IOException; 10 import java.net.URLDecoder; 11 import java.net.URLEncoder; 12 importthe java.text.SimpleDateFormat; 13 is Import java.util.Date; 14 15 @WebServlet ( "/ CookieServlet" ) 16 public class CookieServlet the extends the HttpServlet { . 17 protected void the doPost (the HttpServletRequest Request, the HttpServletResponse Response) throws IOException { 18 is // Get PrintWriter and encoding data format of the stream before setting response message body . 19 response.setHeader ( "Content-type", "text / HTML; charset = UTF-. 8" ); 20 is 21 is // 1. get all the cookies 22 is cookies [] cookies = request.getCookies (); 23 boolean Flag = false ; // no name is called lastTime cookie 24- 25 // 2. traverse the cookie array 26 IF (Cookies =! Null ) { 27 for (Cookie cookie: Cookies) { 28 IF (the Cookie.getName (). the equals ( "lastTime")) { // is not the first access 29 In Flag = to true ; 30 31 is String value = cookie.getValue (); 32 value = URLDecoder.decode (value, "UTF-. 8"); // the URL decoding 33 . response.getWriter () write ( "Welcome back, your last access time is" + value); 34 35 // The value of the cookie value to the login time, resend cookie 36 a Date DATE = new new a Date (); 37 [ the SimpleDateFormat SDF = new new the SimpleDateFormat ( "date YYYY, mM-dd HH: mm: SS" ); 38 is String str_date = sdf.format (dATE); 39 str_date = the URLEncoder.encode (str_date, "UTF-. 8" ); // URL encoding, since str_date contains a space, if no error will be URL-encoded 40 cookie.setValue (str_date); 41 is cookie.setMaxAge (30 * 24 * 60 * 60 ); // set the cookie survival time 42 is response.addCookie (cookie); 43 is BREAK ; 44 is } 45 } 46 is } 47 48 IF (Cookies == null || In Flag == false ) { // first visit 49 response.getWriter () the Write ( "Hello <h1>, welcome to your first visit <h1>." ); 50 51 a Date DATE = new new a Date (); 52 SimpleDateFormat SDF = new new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); 53 String str_date = sdf.format(date); 54 str_date = URLEncoder.encode(str_date, "utf-8"); 55 Cookie cookie = new Cookie("lastTime", str_date); 56 cookie.setMaxAge(30 * 24 *60 *60); 57 response.addCookie(cookie); 58 } 59 60 61 } 62 63 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 64 this.doPost(request, response); 65 } 66 }