码迷,mamicode.com
首页 > 其他好文 > 详细

session和cookie的使用

时间:2017-09-06 22:05:05      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:adc   return   session   http   public   enum   ati   https   ini   

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的使用

标签:adc   return   session   http   public   enum   ati   https   ini   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!