jsp使用cookie实现记住密码的功能

文章来源:http://blog.csdn.net/dracowk/article/details/6887327

这个一个页面模拟的cookie

如果你要实现登录,当用户输入用户名密码时,到控制层用 

Cookie user = new Cookie("user",name+"-"+passward); 加到cookie中,

然后在访问此登录页面时,就先调用 Cookie[] cookies=request.getCookies(); 

看cookie中是否有user这个值,如果有就取得用户名、密码。 


代码示例:

[java]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>   
  2. <%   
  3. String name="nihao";   
  4. String passward="nihao";   
  5. try{   
  6. Cookie user = new Cookie("user",name+"-"+passward);   
  7. user.setMaxAge(60);   
  8. response.addCookie(user);   
  9. Cookie[] cookies=request.getCookies();   
  10. if(cookies!=null){   
  11. for(int i=0;i<cookies.length;i++){   
  12. if(cookies[i].getName().equals("user")){    
  13. name=cookies[i].getValue().split("-")[0];   
  14. passward=cookies[i].getValue().split("-")[1];   
  15. request.setAttribute("name",name);   
  16. request.setAttribute("pass",passward);   
  17. }   
  18. }   
  19. }   
  20. }catch(Exception e){   
  21. e.printStackTrace();   
  22. }   
  23.   
  24. %>   
  25.   
  26. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  27. <html>   
  28. <head>   
  29. <title></title>   
  30.   
  31. <meta http-equiv="pragma" content="no-cache">   
  32. <meta http-equiv="cache-control" content="no-cache">   
  33. <meta http-equiv="expires" content="0">   
  34. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  35. <meta http-equiv="description" content="This is my page">   
  36. <!--   
  37. <link rel="stylesheet" type="text/css" href="styles.css">   
  38. -->   
  39.   
  40. </head>   
  41.   
  42. <body>   
  43. <form action="test10.jsp" method="post">   
  44. <input type="text" value="${name}" name=name /></br>   
  45. <input type="text" value="${pass}" name=pass />   
  46. <input type="submit" value="提交">   
  47. </form>   
  48. </body>   
  49. </html>  







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值