http://www.cnblogs.com/shiyangxt/archive/2008/10/07/1305506.html
cookie存在客户端, session存储在服务器,
cookie的内容主要包括:名字,值,过期时间,路径和域
存取方式:
js
存值: document.cookie ="name=value;path=pathValue;domain=domainValue;secure=secureValue"; 取值:
var cookies = document.cookie.split(";");
cookies[0],cookies[1] ,....
java
cookie存值
LoginInfo loginInfo = new LoginInfo();
loginInfo.setUsername("asd");
loginInfo.setPassword("c8e67359dda4");
Cookie userCookie=new Cookie("loginInfo",loginInfo); userCookie.setMaxAge(30*24*60*60); //存活期为一个月 30*24*60*60 userCookie.setPath("/"); response.addCookie(userCookie);
cookie取值
Cookie[] cookies = request.getCookies(); for(Cookie cookie : cookies){ if(cookie.getName().equals("loginInfo")){ String loginInfo = cookie.getValue(); String username = loginInfo.split(",")[0]; String password = loginInfo.split(",")[1]; request.setAttribute("username", username); request.setAttribute("password", password); } }
session
取值
js
${sessionScope.username}或者${username}
存值
<% session.setAttribute('name', 'value')%>
java
取值
request.getSession().getAttibute("name");
存值
request.getSeesion().getAttribute("name","value")