js和java cookie操作

JS:

jquery.cookie.js

<script>
$(document).ready(function() {
  $.cookie('name', 'gzgd',{expires: 7}); //设置
  var test1 = $.cookie('name');//获取
  alert (test1);
  //$.cookie('name', null); //删除

});

</script>

 

 

JAVA:

public static Cookie FindCookie(String name,HttpServletRequest request) {
Cookie[] cookies = request.getCookies();//这样便可以获取一个cookie数组
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(name)) {
return cookie;
}
}
}
return null;
}

 

//读取cookie
Cookie cookie1 = CommonUtil.FindCookie("miugonavi", request);
String value1 = cookie1.getValue();

//设置

Cookie cookie = new Cookie("miugonavi",value);
cookie.setMaxAge(30*24*3600);
cookie.setPath("/");
response.addCookie(cookie);

 

 

posted @ 2012-12-13 12:05  七郎  Views(2433)  Comments(0Edit  收藏  举报