//登陆java
@RequestMapping("login") public String login(Users user,HttpSession session,Model model,HttpServletResponse response,String pwdcokie) { List<Users> u = dser.ulist(user); if(u.size()>0) { //把用户信息存入session session.setAttribute("user", u.get(0)); if(pwdcokie!=null) { //把用户信息存入cookie //从对象中取出名字 密码 String name=user.getName(); String pwd=user.getPwd(); // new cookie 空间 Cookie cname = new Cookie("name", name); Cookie cpwd = new Cookie("pwd", pwd); //设置cookie过时时间 cname.setMaxAge(60*60); cpwd.setMaxAge(60*60); //设置全路径 cname.setPath("/"); cpwd.setPath("/"); //响应请求 存入cookie response.addCookie(cname); response.addCookie(cpwd); }else { String name=user.getName(); String pwd=user.getPwd(); // new cookie 空间 Cookie cname = new Cookie("name", name); Cookie cpwd = new Cookie("pwd", pwd); //设置cookie过时时间 cname.setMaxAge(0); cpwd.setMaxAge(0); //设置全路径 cname.setPath("/"); cpwd.setPath("/"); //响应请求 存入cookie response.addCookie(cname); response.addCookie(cpwd); } //把用户名放入session session.setAttribute("p", pwdcokie); session.setAttribute("name", u.get(0).getName()); return "main"; } return "login"; }
前台<%java%>web
<% String name=request.getParameter(""); String pwd=request.getParameter(""); Cookie[] cookies=request.getCookies(); for(int i=0;i<cookies.length;i++){ if(cookies[i].getName().equals("name")) { name=cookies[i].getValue(); } } for(int i=0;i<cookies.length;i++){ if(cookies[i].getName().equals("pwd")) { pwd=cookies[i].getValue(); } } //自动登陆(跳转) // response.sendRedirect("main.jsp"); %>
form表单cookie
<form action="/login" method="post"> <table> <tr> <td>用户名:<input type="text" name="name" value=" <%if(name!=null)out.print(name); %>"> </td> </tr> <tr> <td>密码:<input type="password" name="pwd" value=" <%if(pwd!=null)out.print(pwd); %>"> </td> </tr> <tr> <td><input type="checkbox" name="pwdcokie" value="1" ${p=='1'?'checked':''}>记 住密码</td> </tr> <tr> <td><input type="submit" value="登陆" > </td> </tr> </table> </form>