前言
- 在以前学习servlet的时候,当时作的小网站须要登录而且判断信息,当时使用session传值,使用fitter过滤判断,当时感受哇,session咋这么好用,cookie是啥玩意,还不方便。
- 后来在学习ssm作项目遇到须要登录的状态,在html没法使用session,只能学习cookie了,对于cookie只是在爬虫中用过,为了爬去登录后的页面,看来那些网站都是用cookie保持会话的呢。
- 用了cookie发现cookie也很方便。可是js操做cookie很麻烦用jquery就很简单。
前端须要引入
<script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/jquery.cookie.js"></script>
.添加一个"会话cookie"
$.cookie('cookie’name, ‘cookievalue’);
读取cookie
$.cookie(‘the_cookie’);
5.删除cookie
$.cookie(‘cookiename’, null); //经过传递null做为cookie的值便可
还有更多操做请百度。javascript
后端操做demo
@RequestMapping(value="/admin") public String admin(HttpServletRequest request, HttpServletResponse respose) { if(request.getMethod().equals("GET")) { Cookie[] cookies = request.getCookies(); if(cookies!=null &&cookies.length>0)//判断是否已经登录已经登录 { for (Cookie cookie : cookies) { if(cookie.getName().equals("userid")&&cookie.getValue()!=null) { List<Map> map = foodmapper.getadmin(cookie.getValue()); if(map.size()>0) return "admin"; else { return "login"; } } } } return "login"; } else { String username = request.getParameter("username"); List<Map> map = foodmapper.getadmin(username); if(map.size()==0){ return "login";} String password = (String) map.get(0).get("password"); System.out.println(password); if (username.equals(map.get(0).get("username"))&&password.equals(request.getParameter("password"))) { Cookie cookie = new Cookie("userid", username); respose.addCookie(cookie); return "admin"; } else return "default"; } }
对比起来仍是客户端的cookie好用,他就像map一条记录,然后台的更像是cookie数组。须要遍历。html
- 若是对
后端、爬虫、数据结构算法
等感性趣欢迎关注个人我的公众号交流:bigsai
本文分享 CSDN - Big sai。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。前端