手机网上商城-项目经验总结(十)-自动登录
2017-10-07 20:50
501 查看
10.自动登录功能 //登录 public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, IllegalAccessException, InvocationTargetException, ServletException{ Map<String,String[]> map = request.getParameterMap(); User user = new User(); BeanUtils.populate(user,map); if (map.get("autoLogin")!=null){ Cookie username = new Cookie("username", map.get("username")[0]); username.setMaxAge(60*60); Cookie password = new Cookie("password", map.get("password")[0]); password.setMaxAge(60*60); response.addCookie(username); response.addCookie(password); } user = us.login(user); if (user != null){ request.getSession().setAttribute("user", user); response.sendRedirect("/ShopStore/default.jsp"); } else{ request.setAttribute("message", "用户或密码错误!"); request.getRequestDispatcher("/login.jsp").forward(request, response); } } //登出 public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException, IllegalAccessException, InvocationTargetException, ServletException { Cookie[] cookies = request.getCookies(); if (cookies != null){ for (Cookie cookie : cookies) { if (cookie.getName().equals("username") || cookie.getName().equals("password")){ //删除cookie cookie.setMaxAge(0); response.addCookie(cookie);//必须,否则实现不了 } } } request.getSession().removeAttribute("user"); response.sendRedirect("/ShopStore/"); } } 自动登录采用cookie的方式,并且设有有效时间。
相关文章推荐
- 手机网上商城-项目经验总结(十二)-filter定义权限拦截
- 手机网上商城-项目经验总结(二)-创建项目结构
- 手机网上商城-项目经验总结(十一)-修改购物车商品数量
- 手机网上商城-项目经验总结(三)-QueryRunner
- 手机网上商城-项目经验总结(四)-BaseServlet类
- 手机网上商城-项目经验总结(五)-添加商品
- 手机网上商城-项目经验总结(七)-导航栏功能
- 手机网上商城-项目经验总结(八)-商品浏览记录
- 手机网上商城-项目经验总结(九)-邮箱激活注册
- 手机网上商城-项目经验总结(一)-项目开发流程
- 项目之网上商城 购物车 总结 更新中。。。
- 【SSH网上商城项目实战18】过滤器实现购物登录功能的判断
- 【SSH网上商城项目实战30】项目总结(附源码下载地址)
- C# ASP.NET程序员整合Java门户单点登录PHPwind论坛博客软件集成项目经验总结
- 网上商城项目总结 续
- C# ASP.NET程序员整合Java门户单点登录PHPwind论坛博客软件集成项目经验总结
- 李洪强iOS开发本人集成环信的经验总结_08_自动登录补充
- 手机平台项目中的问题及经验总结(一)
- 网上商城项目总结
- C# ASP.NET程序员整合Java门户单点登录PHPwind论坛博客软件集成项目经验总结