前端操作cookie:
var cookie = { set:function(key,val,time){//设置cookie方法 var date=new Date(); //获取当前时间 var expiresDays=time; //将date设置为n天以后的时间 date.setTime(date.getTime()+expiresDays*24*3600*1000); //格式化为cookie识别的时间 document.cookie=key + "=" + val +";expires="+date.toGMTString(); //设置cookie }, get:function(key){//获取cookie方法 /*获取cookie参数*/ var getCookie = document.cookie.replace(/[ ]/g,""); //获取cookie,并且将获得的cookie格式化,去掉空格字符 var arrCookie = getCookie.split(";") //将获得的cookie以"分号"为标识 将cookie保存到arrCookie的数组中 var tips; //声明变量tips for(var i=0;i
后端操作cookie:
package gac.xdp.cookie;import java.io.IOException;import java.io.PrintWriter;import java.util.Date;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * @author gacl * cookie实例:获取用户上一次访问的时间 */public class CookieDemo01 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置服务器端以UTF-8编码进行输出 response.setCharacterEncoding("UTF-8"); //设置浏览器以UTF-8编码进行接收,解决中文乱码问题 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); //获取浏览器访问访问服务器时传递过来的cookie数组 Cookie[] cookies = request.getCookies(); //如果用户是第一次访问,那么得到的cookies将是null if (cookies!=null) { out.write("您上次访问的时间是:"); for (int i = 0; i < cookies.length; i++) { Cookie cookie = cookies[i]; if (cookie.getName().equals("lastAccessTime")) { Long lastAccessTime =Long.parseLong(cookie.getValue()); Date date = new Date(lastAccessTime); out.write(date.toLocaleString()); } } }else { out.write("这是您第一次访问本站!"); } //用户访问过之后重新设置用户的访问时间,存储到cookie中,然后发送到客户端浏览器 Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis()+"");//创建一个cookie,cookie的名字是lastAccessTime //将cookie对象添加到response对象中,这样服务器在输出response对象中的内容时就会把cookie也输出到客户端浏览器 response.addCookie(cookie); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}