您的位置:首页 > 其它

使用cookie 记录登陆

2010-09-28 13:52 316 查看
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"/>  

参看: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&Uin=309391670&Site=通达欢迎您&Menu=yes' target='_blank'>

<img src="resource/images/pa.gif" width="61" height="16" border="0" alt="点击这里给我发消息"></a></td>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: