demand:
1. Visit 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
analysis:
1 . Cookie can be accomplished using
2 in the server Servlet determine whether there is a cookie called the lastTime.
2. 1 are: not the first visit to
1. Response data: Welcome back, your last visit time: 2019 October 4 11:50:20
2. write back Cookie: lastTime = 2019 Nian October 4 11:50:01
2.2 is not: It is the first visit to
a response data: Hello, welcome to your first visit
2. write back Cookie: lastTime = 2019 Nian 10 Yue 4 Ri 11:50:01
Code:
1 import javax.servlet.ServletException; 2 import javax.servlet.annotation.WebServlet; 3 import javax.servlet.http.Cookie; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.IOException; 8 import java.net.URLDecoder; 9 import java.net.URLEncoder; 10 import java.text.SimpleDateFormat; 11 import java.util.Date; 12 is 13 is / ** 14 remember the user login time Case 15 * / 16 @WebServlet ( "/ CookieServlet" ) . 17 public class CookieServlet the extends the HttpServlet { 18 is protected void the doPost (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { . 19 // Setup response message body formats, and encoded data 20 is the response.setContentType ( "text / HTML; charset = UTF-. 8" ); 21 is 22 is // . 1 accessories Cookie 23 is cookies [] = cookies request.getCookies (); 24 Boolean In Flag = to false ; // default is no cookie lastTime 25 // 2. cookie traverse the array 26 is IF (= Cookies! Null && cookies.length> 0 {) 27 for (cookie cookies: Cookies) { 28 // . 3 acquires All cookie names 29 String name = the Cookie.getName (); 30 // 4. determines whether the name is lastTime 31 is iF ( "lastTime" .equals (name)) { 32 // there cookie, not the first access 33 is in Flag = to true ; 34 is 35 // Get the cookie value 36 // response data 37 // Get the cookie value, s time 38 is String value = cookie.getValue (); 39 40 System.out.println ( "before decoding:" + value); 41 is // the URL decoder 42 is value URLDecoder.decode = (value, "UTF-. 8" ); 43 is System.out.println ( "decoded:" + value); 44 is 45 46 is // set the cookie value 47 // Get string current time, reset the cookie value, re-send cookie 48 49 Date date = new Date(); 50 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); 51 String str_date = sdf.format(date); 52 53 System.out.println("编码前:"+str_date); 54 //URL 编码 55 str_date = URLEncoder.encode(str_date, "utf-8"); 56 System.out.println("编码后:"+str_date); 57 58 cookie.setValue(str_date); 59 60 // set the cookie survival time of 61 cookie.setMaxAge (60 * 60 ); 62 response.addCookie (cookie); 63 64- 65 response.getWriter () the Write ( "<h1> Welcome back, your last visit time: "+ value +" </ h1 of> " ); 66 67 BREAK ; 68 } 69 } 70 } 71 is 72 IF (Cookies == null || cookies.length == == 0 || In Flag to false ) { 73 is // not, first visit 74 75 //Set the cookie value 76 // get the current time string set cookie value, transmits cookie 77 78 a Date DATE = new new a Date (); 79 the SimpleDateFormat SDF = new new the SimpleDateFormat ( "date YYYY, MM-dd HH: mm: ss" ); 80 String str_date = sdf.format (DATE); 81 Cookie the cookie = new new Cookie ( "lastTime" , str_date); 82 83 // resolution time there are special characters using the URL encoding 84 System.out.println ( "coding ago: "+ str_date); 85 // the URL of encoding 86 = the URLEncoder.encode str_date (str_date, "UTF-. 8" ); 87 System.out.println ( "encoded:" + str_date); 88 89 cookie.setValue (str_date); 90 91 is // set the cookie survival time of 92 cookie.setMaxAge (60 * 60 ); 93 response.addCookie (Cookie); 94 95 . response.getWriter () Write ( "<h1 of> Welcome you first access </ h1 of>" ); 96 } 97 } 98 99 protected void doGet (Request the HttpServletRequest, HttpServletResponse the Response) throws ServletException, IOException { 100 this.doPost(request, response); 101 } 102 }