java写cookie_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java写cookie

java写cookie

 2011/12/27 9:07:42  jw_long  http://jw-long.iteye.com  我要评论(0)
  • 摘要:/***创建cookie**@paramcookieName保存的cookieKey*@paramparam保存的cookieValue*@paramtime时效(单位:秒)*@paramresponse*/publicstaticbooleanaddCookie(finalStringcookieName,finalStringparam,inttime,HttpServletResponseresponse){try
  • 标签:Java Cookie

/**
??? ?* 创建cookie
??? ?*
??? ?* @param cookieName保存的cookieKey
??? ?* @param param保存的cookieValue
??? ?* @param time时效(单位:秒)
??? ?* @param response
??? ?*/
??? public static boolean addCookie(final String cookieName,
??? ??? ??? final String param, int time, HttpServletResponse response) {
??? ??? try {
??? ??? ??? // 创建保存用户的cookie
??? ??? ??? Cookie cookiesUser = new Cookie(cookieName, param);
??? ??? ??? // 设置访问域
??? ??? ??? cookiesUser.setPath("/");
??? ??? ??? // 设定自动登录期限为二周
??? ??? ??? cookiesUser.setMaxAge(time);
??? ??? ??? //
??? ??? ??? response.addCookie(cookiesUser);
??? ??? } catch (Exception e) {
??? ??? ??? e.printStackTrace();
??? ??? ??? return false;
??? ??? }
??? ??? return true;
??? }

??? /**
??? ?* 清除cookie
??? ?*
??? ?* @param cookieName要清除的cookie名
??? ?* @param response
??? ?* @return
??? ?*/
??? public static boolean deleteCookie(final String cookieName,
??? ??? ??? HttpServletResponse response) {
??? ??? try {
??? ??? ??? Cookie cookie = new Cookie(cookieName, null);
??? ??? ??? cookie.setPath("/");
??? ??? ??? // 时效为0时即时删除
??? ??? ??? cookie.setMaxAge(0);
??? ??? ??? response.addCookie(cookie);
??? ??? } catch (Exception e) {
??? ??? ??? e.printStackTrace();
??? ??? ??? return false;
??? ??? }
??? ??? return true;
??? }

发表评论
用户名: 匿名