`

session的登录与注销

阅读更多
	protected void clearSession() {

		SessionMap sessionMap = (SessionMap) ActionContext.getContext()
				.getSession();
		try {
			sessionMap.clear();
		} catch (Exception e) {
			//
		}

		HttpSession httpSession = this.getServletRequest().getSession();
		try {
			httpSession.invalidate();
		} catch (Exception e) {
			//
		}
	}

 

 

doLogin

 

public String doLogin() {
		clearSession();
		String loginUsername = this.getLoginUsername();
		String loginPassword = this.getLoginPassword();
		if (StringUtils.isBlank(loginUsername)) {
			setMessage("用户名必填项");
			return INPUT;
		} else if (StringUtils.isBlank(loginPassword)) {
			setMessage("密码必填项");
			return INPUT;
		}

		try {
			setSessionAttribute(SESSION_ATTRIBUTE_KEY_USER,user);
					setCookiesAttribute(COOKIES_ATTRIBUTE_KEY_EMPLOYEE_NO, loginUsername);
			return "home";
		} catch (WebApplicationRuntimeException e) {
			this.setMessage(e.getMessage());
		}

		return INPUT;
	}

 

 

loginout

 

	public String execute() {
		try {
			clearSession();
			ReserverCookie reserverCookie = new ReserverCookie();
			reserverCookie.removeCookie(this.getServletRequest(), getServletResponse());
		} catch (Exception e) {

		}
		return "login";
	}

 

 

	public void removeCookie(HttpServletRequest request,
			HttpServletResponse response) {
		Cookie cookie = getCookie(request);
		if (cookie != null) {
			cookie = new Cookie(COOKIE_NAME, "");
			cookie.setMaxAge(0);
			cookie.setValue("");
			cookie.setPath("/");
			cookie.setDomain(CSAIR_COM);
			response.addCookie(cookie);
		}
	}

 

分享到:
评论
2 楼 cczakai 2013-04-24  
protected void setSessionAttribute(String key, Object value) {
HttpSession session = this.getServletRequest().getSession();
session.setAttribute(key, value);
}
1 楼 javatozhang 2013-03-23  
哥们儿你这两个方法是什么:
  setSessionAttribute(SESSION_ATTRIBUTE_KEY_USER,user); 
  setCookiesAttribute(COOKIES_ATTRIBUTE_KEY_EMPLOYEE_NO, loginUsername); 

最近在做用户登录和注销参考一下你的意见和想法,谢谢!

相关推荐

Global site tag (gtag.js) - Google Analytics