cookie实现登陆页面保存用户名
来源:互联网 发布:手机4g网络测速在线 编辑:程序博客网 时间:2024/05/21 04:21
密码也可以保存,但我没实现出来
1.首先是用户名,密码的input 和保存状态的checkbox
<input class="ipt" type="text" name='username' value='${name}'/><input class="ipt" type='password' name='password' id='password' /><input style=" margin-left:60px; margin-right:10px; " type="checkbox" name="rememberMe" id="rememberMe" />
没找到怎么保存checkbox的状态,我就自己写了个
<script>window.onload = function() {if ('${name}' != '') {document.getElementById('rememberMe').checked = true;} else {document.getElementById('rememberMe').checked = false;}}</script>
2.然后在jsp顶部加入java代码,用于读取cookie
<%String name = "";String psw = "";String checked = "";Cookie[] cookies = request.getCookies();if(cookies != null && cookies.length>0){for(int i =0; i<cookies.length; i++){if(cookies[i].getName().equals("name")){name=cookies[i].getValue();request.setAttribute("name",name);}if(cookies[i].getName().equals("psw")){psw=cookies[i].getValue();request.setAttribute("psw",psw);}}}%>
3.其次,在登陆到后台验证完毕密码后面加入
//处理CookieaddCookie(username , pwd ,response ,request);
下面是addCookie这个方法
/**Cookie的实现 * @throws UnsupportedEncodingException **/private void addCookie(String name, String password,HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {if(StringUtils.isNotBlank(name)&&StringUtils.isNotBlank(password)){//创建Cookie//Cookie nameCookie=new Cookie("name",URLEncoder.encode(name,"utf-8"));Cookie nameCookie=new Cookie("name",name);Cookie pswCookie=new Cookie("psw",password);//设置Cookie的父路径nameCookie.setPath(request.getContextPath()+"/");pswCookie.setPath(request.getContextPath()+"/");//获取是否保存CookieString rememberMe=request.getParameter("rememberMe");if(rememberMe==null){//不保存CookienameCookie.setMaxAge(0);pswCookie.setMaxAge(0);}else{//保存Cookie的时间长度,单位为秒nameCookie.setMaxAge(7*24*60*60);pswCookie.setMaxAge(7*24*60*60);}//加入Cookie到响应头response.addCookie(nameCookie);response.addCookie(pswCookie);}}}
参照了:http://www.2cto.com/kf/201405/302677.html 和http://blog.sina.com.cn/s/blog_6d187d2701019ayb.html
0 0
- cookie实现登陆页面保存用户名
- Jquery 登陆页面 Cookie记住用户名密码 (可设定保存时间)
- 登录页面,用cookie实现 记住用户名
- python+selenium实现登陆保存cookie
- cookie 保存用户名密码
- Cookie保存用户名密码
- Cookie学习,保存用户名
- jsp,action使用cookie实现记住登陆用户名
- java - 利用cookie实现记住用户名方便之后登陆
- 使用Jquery cookie插件实现用户名和密码的保存
- servlert实现保存cookie(记住用户名和密码)
- 登陆页面实现保存帐号密码功能
- 登陆页面的用户信息保存实现
- 登陆页面实现保存帐号密码功能
- 登陆页面实现保存帐号密码功能
- 登陆页面实现保存帐号密码功能
- 保存登陆用户名和密码
- cookie保存用户名和密码
- oracle 11g不能导出空表的多种解决方法
- eclipse中导入jar包的源码
- JAVA下wordpress加密
- ap0100at_ds移植步骤
- 2012年5月SAT香港真题解析
- cookie实现登陆页面保存用户名
- XMPP协议实现原理介绍
- Volley来实现网络请求
- ROS学习手记13 -- 完成了初级的教程以后
- Swap Nodes in Pairs
- app后台设计总结
- 详解const
- Project Euler:Problem 65 Convergents of e
- 虚函数,虚表