使用cookie 记录登陆

来源:互联网 发布:软件代码模型 编辑:程序博客网 时间:2024/05/19 14:51

 public static final String Constants.COOKIE_REMEMBERME_KEY="user";

public class StringUtils{

 

 public booblean isNotEmpty(String word){

 

  if(wrod.equals("")){

   return true;

  }else{

   return false;

  }

 }

 

/记录客户端Cookie中

                if (StringUtils.isNotEmpty(rememberMe)) {

                    if (rememberMe.equals("true")) {

                        Cookie cookie = new Cookie(Constants.COOKIE_REMEMBERME_KEY, user.getUserName() + "==" + user.getPassword());

                        cookie.setMaxAge(60 * 60 * 24 * 14);

                        cookie.setPath("/");

                        response.addCookie(cookie);

                    }

                } 

 

User curUser = (User)session.getAttribute(Constants.USER);

if(curUser==null)

{

Cookie[] cookies = request.getCookies();

if (cookies != null) {

for (Cookie cookie : cookies) {

if (Constants.COOKIE_REMEMBERME_KEY.equals(cookie.getName())) {

String value = cookie.getValue();

if (StringUtils.isNotBlank(value)) {

String[] split = value.split("==");

String userName = split[0];

String password = split[1];

try {

User user = userJdbc.getByUserName(userName);

if (null != user) {

if (user.getPassword().equalsIgnoreCase(password)) {

session.setAttribute(Constants.USER, user);

request.getSession().setAttribute("autoAuctionState", user.getAutoAuctionState());

}

}

} catch (Exception e) {

//response.sendRedirect("/index.ac");

}

} else {

//response.sendRedirect("/index.ac");

}

//response.sendRedirect("/index.ac");

}

}

}

 

// 客户端从本地查找服务器端发送过来的cookie,有没有user,如果有就把以下那个值负

<%

 String userName = null;

 String password = null;  

 

 Cookie[] cookies = request.getCookies();

 for (int i = 0; cookies != null && i < cookies.length; i++) {

     if(cookies[i].getName().equals("user")){

      userName = cookies[i].getValue().split("==")[0];

      password = cookies[i].getValue().split("==")[1];

     }

 }  

 if(userName == null){

  name = "";

 }

 if(password == null){

  pass = "";

 }

%>

用户名:<input type="text" name="name" size="15" value="<%=userName %>" />

密码:<input type="password" name="pass"  size="15"  value="<%=password %>" />

记住密码:<input type="checkbox" name="ck" value="1"/> &nbsp;

 

参看:http://blog.csdn.net/Jerry_BJ/archive/2010/06/27/5697532.aspx

 

外部qq

<td height="30" align="center">

  <a href=' http://wpa.qq.com/msgrd?V=1&amp;Uin=309391670&amp;Site=通达欢迎您&amp;Menu=yes' target='_blank'>

<img src="resource/images/pa.gif" width="61" height="16" border="0" alt="点击这里给我发消息"></a></td>

原创粉丝点击