struts2 cookie清除
(2016-5-17 21:26:38)
添加cookie
cookie phone;
phone = new Cookie("userHp", hp);
phone.setMaxAge(60 * 60 * 24 * 365);
phone.setPath("/");
ServletActionContext.getResponse().addCookie(phone);
获取cookie
public String getCookieValue(Cookie[] cookies, String str) {
String result = null;
for (Cookie cookie : cookies) {
if (cookie.getName().equals(str)) {
result = cookie.getValue();
break;
}
}
return result;
}
通过请求获取页面上的cookie,分别读取cookie值
// 删除cookie
public Cookie delCookie(HttpServletRequest request) {
request = ServletActionContext.getRequest();
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for(int i=0;i<cookies.length;i++)
{
if("userName".equals(cookies[i].getName()) ||"userHp".equals(cookies[i].getName()) || "password".equals(cookies[i].getName())) {
Cookie cookie = new Cookie(cookies[i].getName(), "");
cookie.setMaxAge(0);
cookie.setPath("/");
ServletActionContext.getResponse().addCookie(cookie);
//return cookie;
}
}
}
return null;
}
然后获取session调用删除cookie的方法就可以删除
HttpSession session = ServletActionContext.getRequest().getSession();
if (session != null)
session.removeAttribute("userName");
session.removeAttribute("userHp");
delCookie(this.getRequest());
if(password.trim().matches("^[\u4e00-\u9fa5]+$")&&!password.trim().matches("^[[email protected]]+$")){
this.addActionError("请重新输入密码,不能用特殊符号和中文!");
return ERROR;
}
if(!hp.trim().matches("1[3458][0-9]{9}")){
this.addActionError("手机号有问题,请重新输入!");
return ERROR;
}
if(!name.trim().matches("^[a-zA-Z0-9_\u4e00-\u9fa5]+$")){
this.addActionError("用户名不能用特殊符号!");
return ERROR;
}