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.其次,在登陆到后台验证完毕密码后面加入
- //处理Cookie
- addCookie(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()+"/");
- //获取是否保存Cookie
- String rememberMe=request.getParameter("rememberMe");
- if(rememberMe==null){//不保存Cookie
- nameCookie.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);
- }
- }
- }
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.其次,在登陆到后台验证完毕密码后面加入
- //处理Cookie
- addCookie(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()+"/");
- //获取是否保存Cookie
- String rememberMe=request.getParameter("rememberMe");
- if(rememberMe==null){//不保存Cookie
- nameCookie.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);
- }
- }
- }
- 这是我借鉴CSDN一位前辈的