Created
April 17, 2013 13:02
-
-
Save wdfx100/5404104 to your computer and use it in GitHub Desktop.
#cookie的添加#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| login.java: | |
| if(rm != null){ | |
| Cookie usernameCookie =new Cookie("username", username); | |
| usernameCookie.setDomain("www.wdfx100.com"); | |
| usernameCookie.setPath("/"); | |
| usernameCookie.setMaxAge(60*60*24); | |
| Cookie passwordCookie =new Cookie("password", password); | |
| passwordCookie.setDomain("www.wdfx100.com"); | |
| passwordCookie.setPath("/"); | |
| passwordCookie.setMaxAge(60*60*24); | |
| response.addCookie(usernameCookie); | |
| response.addCookie(passwordCookie); | |
| } | |
| HttpSession session = request.getSession(); | |
| session.setAttribute("user",user); | |
| response.sendRedirect("list"); | |
| index.java | |
| //读取cookie | |
| Cookie[] cookies = request.getCookies(); | |
| String username=null; | |
| String password=null; | |
| if(cookies!=null){ | |
| for(Cookie c:cookies){ | |
| if(c.getName().equals("username")){//多个键与值配对"username"为放进去的值 | |
| username=c.getValue(); | |
| } | |
| if(c.getName().equals("password")){ | |
| password=c.getValue(); | |
| } | |
| } | |
| if(username!=null && password!=null){ | |
| //获取user | |
| User user = new UserDao().findByUserName(username); | |
| if(user!=null && password.equals(user.getPassword())){ | |
| HttpSession session=request.getSession(); | |
| session.setAttribute("user", user); | |
| response.sendRedirect("list"); | |
| return; | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment