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);
		}
	}

猜你喜欢

转载自cczakai.iteye.com/blog/1575895