首页 > 其他 > 详细

session和cookie的使用

时间:2017-09-06 21:46:55      阅读:279      评论:0      收藏:0      [点我收藏+]
private String getHeaders(HttpServletRequest request) {
Enumeration en = request.getHeaderNames();
String pasession = "";
while (en.hasMoreElements()) {
String key = en.nextElement().toString();
String value = request.getHeader(key);
if (StringUtils.isNotEmpty(value) && (value.indexOf("PASESSION=") != -1 || value.indexOf("pasession=") != -1)) {
int beginIndex = value.indexOf("PASESSION=");
if (beginIndex < 0) {
beginIndex = value.indexOf("pasession=") + 10;
} else {
beginIndex = beginIndex + 10;
}
String subStr = value.substring(beginIndex);
int endIndex = subStr.indexOf(";");
if (endIndex < 0) {
pasession = subStr;
} else {
pasession = subStr.substring(0, endIndex);
}
break;
}
}
return pasession;
}

private String getPasession(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
String PASESSION = "";
if (null != cookies) {
for (int i = 0; i < cookies.length; i++) {
if ((cookies[i].getName()).equals("PASESSION")) {
PASESSION = cookies[i].getValue();
}
}
}
return PASESSION;
}

public class CookieUtils {

public static Cookie getCookieByName(Cookie[] cookies, String name) {
Map<String, Cookie> cookieMap = ReadCookieMap(cookies);
if (cookieMap.containsKey(name)) {
Cookie cookie = cookieMap.get(name);
return cookie;
} else {
return null;
}
}

/**
* 将cookie封装到Map里面
*
* @return
*/
private static Map<String, Cookie> ReadCookieMap(Cookie[] cookies) {
Map<String, Cookie> cookieMap = new HashMap<>();
if (null != cookies) {
for (Cookie cookie : cookies) {
cookieMap.put(cookie.getName(), cookie);
}
}
return cookieMap;
}

// 创建cookie
public static Cookie createCookie(String cookieName, String cookieValue, int maxAge) {
Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setMaxAge(maxAge);
cookie.setPath("/");
return cookie;
}
}

session和cookie的使用

原文:http://www.cnblogs.com/koushr/p/5873465.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!