SSH中的重构 cookies的管理
SSH中的重构 cookies的管理
目的:集中管理cookies
做法:建造一个类单独管理cookies
下面是一个简单的例子保存用户名(两个星期的时效)
public class CookieInfo { public static final String USERNAME = "USERNAME"; public static final int TWOWEEK = 60 * 60 * 24 * 30; public static String getUserName(HttpServletRequest request) { return getCookie(request, USERNAME); } public static void addUserName(HttpServletResponse response, String value) { addCookie(response, USERNAME, value); } public static void delUserName(HttpServletRequest request, HttpServletResponse response) { delCookie(request,response,USERNAME); } private static void addCookie(HttpServletResponse response, String key, String value) { Cookie cookie = new Cookie(key, java.net.URLEncoder.encode(value)); cookie.setMaxAge(TWOWEEK); response.addCookie(cookie); } private static String getCookie(HttpServletRequest request, String key) { if (request.getCookies() == null) return ""; for (Cookie cookie : request.getCookies()) { if (key.equals(cookie.getName())) { return java.net.URLDecoder.decode(cookie.getValue()); } } return ""; } private static void delCookie(HttpServletRequest request,HttpServletResponse response,String key) { if (request.getCookies() == null) return; for (Cookie cookie : request.getCookies()) { if (key.equals(cookie.getName())) { cookie.setMaxAge(0); response.addCookie(cookie); } } } }
相关推荐
houmenghu 2020-11-17
kentrl 2020-11-10
逍遥友 2020-10-26
jincheng 2020-09-01
Blueberry 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
阳光之吻 2020-08-03
PkJY 2020-07-08
hzyuhz 2020-07-04
89407707 2020-06-27
服务器端攻城师 2020-06-26
阳光岛主 2020-06-25
笨重的蜗牛 2020-06-20
xuanwenchao 2020-06-14
Lophole 2020-06-13
明瞳 2020-06-12
songerxing 2020-06-11