首页 > 代码库 > js、Java后台设置cookie
js、Java后台设置cookie
概述:小开心下,又学到了点东西
JS设置cookie
//添加时间函数
function SetCookie(name, value){
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString(); //又学的一个重点
}
//获取时间函数
function GetCookie(name){
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (arr != null) return unescape(arr[2]); return null;
}
//删除时间函数
function DelCookie(name){
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = GetCookie(name);
if (cval != null) document.cookie = name + "=" + cval + ";path=/;expires=" + exp.toGMTString();
}
Java后台设置COOKIE和取值COOKIE
/**
* 名片登录请求
* @param modelMap
* @param session
* @param response
* @param account
* @param password
* @param autoLogin
* @return
*/
@RequestMapping(value = "http://www.mamicode.com/crm_micro_business_card_login_card.do")
public String login(ModelMap modelMap,HttpSession session,HttpServletResponse response,@RequestParam(value = "http://www.mamicode.com/account",required=false) String account,
@RequestParam(value = "http://www.mamicode.com/password",required=false) String password,@RequestParam(value = "http://www.mamicode.com/autoLogin",required=false) String autoLogin){
String forward = "redirect:/crm_micro_business_card_businessCardManager_card.do";
try {
CrmMicroBusinessCardVO crmMicroBusinessCardVO = mpBasInfoService.doLogin(account,password);
Cookie ckUsername, ckSessionId;
if(autoLogin==null){
session.setAttribute("crmMicroBusinessCardVO", crmMicroBusinessCardVO);
forward = forward+"?id="+crmMicroBusinessCardVO.getId();
}else if(autoLogin.equals("on")){
ckUsername = new Cookie("autoLoginUser",account);
ckUsername.setMaxAge(60*60*24*30);
response.addCookie(ckUsername); //保存登录用户名到COOKIE
String sessionId = session.getId();
ckSessionId = new Cookie("sessionId",sessionId);
ckSessionId.setMaxAge(60*60*24*30);
response.addCookie(ckSessionId); //保存登录sessionId到COOKIE
MpAccountSessionId mpAccountSessionId = new MpAccountSessionId();
mpAccountSessionId.setAccount(account);
mpAccountSessionId.setSessionId(sessionId);
mpBasInfoService.saveSessionId(mpAccountSessionId); //保存登录用户名和sessionId到数据库
session.setAttribute("crmMicroBusinessCardVO", crmMicroBusinessCardVO);
forward = forward+"?id="+crmMicroBusinessCardVO.getId();
}
} catch (BusinessCardException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
modelMap.put("errorMsg",e.getMessage());
modelMap.put("account",account);
modelMap.put("password",password);
forward = "/tools/businessCardLogin";
}
return forward;
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//To change body of implemented methods use File | Settings | File Templates.
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpSession session = request.getSession(true);
String account = "";
String sessionId = ""; // 此sessionid是上次用户登录时保存于用户端的识别码,用于用户后续访问的自动登录。不是本次访问的session id。
Cookie[] cookies;
CookieManager cm = new CookieManager(); // CookieManager是一个自定义的类,用于从Cookie数组中查找并返回指定名称的Cookie值。
boolean isAutoLogin;
// 如果session中没有user对象,则创建一个。
CrmMicroBusinessCardVO crmMicroBusinessCardVO = (CrmMicroBusinessCardVO) session.getAttribute("crmMicroBusinessCardVO");
// 如果user对象的username为"",表示用户未登录。则执行自动登录过程。
// 否则不自动登录。
if (crmMicroBusinessCardVO == null) {
crmMicroBusinessCardVO = new CrmMicroBusinessCardVO();
// 检查用户浏览器是否发送了上次登录的用户名和sessionid,
// 如果是,则为用户自动登陆。
cookies = request.getCookies();
if(cookies!=null&&cookies.length>0){
account = cm.getCookieValue(cookies, "autoLoginUser");
sessionId = cm.getCookieValue(cookies, "sessionId");
account = "".equals(account)?null:account;
sessionId = "".equals(sessionId)?null:sessionId;
Map<String,Object> map = new HashMap<String,Object>();
map.put("account", account);
map.put("sessionId",sessionId);
boolean isExist = basDao.isExist(MpAccountSessionId.class,map);
if (isExist) {
MpBasInfo userVO = new MpBasInfo();
userVO = basDao.queryOne(MpBasInfo.class,"account",account);
if(userVO!=null){
CrmMicroBusinessCardVO crmMicroBusinessCardVO1 = new CrmMicroBusinessCardVO();
UtilBean.copyBeanAllSimpleProperties(crmMicroBusinessCardVO1,userVO,false);
session.setAttribute("crmMicroBusinessCardVO", crmMicroBusinessCardVO1); // 将user bean添加到session中。
}
}
}
}
filterChain.doFilter(servletRequest, servletResponse);
}
public class CookieManager {
public static String getCookieValue(Cookie cookies[],String cookieName){
String cookieValuehttp://www.mamicode.com/= "";
for(int i=0;i<cookies.length;i++){
Cookie cookie = cookies[i];
if(cookie.getName().equals(cookieName)){
cookieValue = http://www.mamicode.com/cookie.getValue().toString();
break;
}
}
return cookieValue;
}
本文出自 “小博客” 博客,请务必保留此出处http://9686567.blog.51cto.com/9676567/1606197
js、Java后台设置cookie