标签:style blog http io color os ar 使用 java
1 package com.yyz.cookie; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.Date; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.Cookie; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 public class CookieDemo extends HttpServlet { 14 // 网站首页:可以实现显示用户上次访问时间 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 response.setContentType("text/html;charset=UTF-8"); 18 PrintWriter out = response.getWriter(); 19 out.write("这是网站首页!!<br><br>"); 20 out.write("您上次访问的时间是:"); 21 // 得到上次访问时间 22 Cookie[] cookies = request.getCookies(); 23 for (int i = 0; cookies != null && i < cookies.length; i++) { 24 Cookie cookie = cookies[i]; 25 if (cookie.getName().equals("lastAccessTime")) { 26 Long time = Long.parseLong(cookie.getValue()); 27 Date d = new Date(time); 28 out.write(d.toLocaleString()); 29 } 30 } 31 // 给用户以cookie的形式送最新的时间 32 Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis() 33 + ""); 34 cookie.setMaxAge(10000); 35 response.addCookie(cookie); 36 37 } 38 39 public void doPost(HttpServletRequest request, HttpServletResponse response) 40 throws ServletException, IOException { 41 42 doGet(request, response); 43 } 44 45 }
标签:style blog http io color os ar 使用 java
原文地址:http://www.cnblogs.com/yyz666/p/4059833.html