bang老師 真的是需要你幫忙了,是有關Struts2 action中操作cookie

tianhandigeng發表於2010-12-26
我做自動登入的功能,是這樣操作的:

package com.zuwoba.presentation.action.account;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionContext;
import com.zuwoba.model.User;
import com.zuwoba.model.UserSession;
import com.zuwoba.presentation.action.base.BaseAction;
import com.zuwoba.util.EmailLoginAddress;
import com.zuwoba.util.MD5_Encoding;

/**
 * @project_name zuwoba
 * @file_name LoginAction.java
 * @author tianhandigeng
 * @version Oct 25, 2010 2:53:04 PM
 * @declaration
 */
public class LoginAction extends BaseAction implements ServletRequestAware,ServletResponseAware{
	private String email_username;
	private String password;
	private String captcha;
	private String autologin;

	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	// 提示視窗
	private String hint;
	private String tip;

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAutologin() {
		return autologin;
	}

	public void setAutologin(String autologin) {
		this.autologin = autologin;
	}

	public String getEmail_username() {
		return email_username;
	}

	public void setEmail_username(String email_username) {
		this.email_username = email_username;
	}

	public String getHint() {
		return hint;
	}

	public void setHint(String hint) {
		this.hint = hint;
	}

	public String getCaptcha() {
		return captcha;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

	public void setServletRequest(HttpServletRequest servletRequest) {
        this.request=servletRequest;		
	}

	public void setServletResponse(HttpServletResponse servletResponse) {
		this.response=servletResponse;
	}
	
	@SuppressWarnings("unchecked")
	public String execute() {
////		HttpServletResponse response = (HttpServletResponse) ActionContext
////				.getContext().get(StrutsStatics.HTTP_RESPONSE);
////		HttpServletRequest request = (HttpServletRequest) ActionContext
////				.getContext().get(StrutsStatics.HTTP_REQUEST);
////		HttpSession session = request.getSession();
//		HttpServletResponse response=(HttpServletResponse)ServletActionContext.getResponse();
//		HttpServletRequest request=(HttpServletRequest)ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		
		
		//獲得域名
//		String host=request.getServerName();
		String host="ceshi";

		email_username = this.getEmail_username().trim();
		password = this.getPassword().trim();
		captcha = this.getCaptcha().trim().toLowerCase();
		autologin = this.getAutologin();

		// 密碼加密
		MD5_Encoding md5 = new MD5_Encoding();
		password = md5.getMD5ofStr(password);

		if (!captcha.equals(session.getAttribute("idcode"))) {
			this.setTip("驗證碼錯誤");
			this.setHint("failed");
			return INPUT;
		} else {
			// Email登入
			User user1 = userService.findUserByEmailPassword(email_username,
					password);

			if (user1 != null) {
				if ("N".equals(user1.getEnable())) {// 郵件登陸但沒有啟用
					String emailAddress = EmailLoginAddress
							.getEmialLoginAddress(email_username);

					session.setAttribute("email", email_username);
					session.setAttribute("secret", user1.getSecret());
					session.setAttribute("tempname", user1.getUsername());
					session.setAttribute("emailAddress", emailAddress);

					return "unverified";
				} else {
					// 處理自動登入
					if (autologin != null) {
						// 建立兩個cookie物件
						// 一個cookie記錄使用者名稱,另一個記錄唯一的驗證碼
						// 並將此驗證碼寫入資料庫,以備使用者返回時查詢(防止偽造cookie)
						Cookie cookie1 = new Cookie("SESSION_LOGIN_USERNAME", user1
								 .getUsername());
						cookie1.setMaxAge(60 * 60 * 24 * 14);// 設定cookie有效期為2周
						cookie1.setPath("/");
						cookie1.setDomain(host);
						response.addCookie(cookie1);

						String sessionid = session.getId();
						Cookie cookie2 = new Cookie("SESSION_ID", sessionid);
						cookie2.setMaxAge(60 * 60 * 24 * 14);
						cookie2.setPath("/");
						cookie2.setDomain(host);
						response.addCookie(cookie2);

						// 在資料庫中插入相應記錄
						UserSession userSession = new UserSession();
						userSession.setUsername(user1.getUsername());
						userSession.setSessionid(sessionid);
						userService.addUserSession(userSession);
					}
					session.setAttribute("user", user1);
					return SUCCESS;
				}
			} else {
				// 使用者名稱登陸
				User user2 = userService.findUserByUserNamePassword(
						email_username, password);
				if (user2 != null) {
					if ("N".equals(user2.getEnable())) {// 使用者名稱登陸但沒有啟用
						String emailAddress = EmailLoginAddress
								.getEmialLoginAddress(email_username);

						session.setAttribute("email", email_username);
						session.setAttribute("secret", user2.getSecret());
						session.setAttribute("tempname", user2.getUsername());
						session.setAttribute("emailAddress", emailAddress);

						return "unverified";
					} else {

						// 處理自動登入
						if (autologin != null) {
							// 建立兩個cookie物件
							// 一個cookie記錄使用者名稱,另一個記錄唯一的驗證碼
							// 並將此驗證碼寫入資料庫,以備使用者返回時查詢(防止偽造cookie)
							Cookie cookie1 = new Cookie("SESSION_LOGIN_USERNAME", user2
									 .getUsername());
							cookie1.setMaxAge(60*60*24*14);// 設定cookie有效期為2周
							cookie1.setPath("/");
							cookie1.setDomain(host);
							response.addCookie(cookie1);

							String sessionid = session.getId();
							Cookie cookie2 = new Cookie("SESSION_ID", sessionid);
							cookie2.setMaxAge(60 * 60 * 24 * 14);
							cookie2.setPath("/");
							cookie2.setDomain(host);
							response.addCookie(cookie2);

							// 在資料庫中插入相應記錄
							UserSession userSession = new UserSession();
							userSession.setUsername(user2.getUsername());
							userSession.setSessionid(sessionid);
							userService.addUserSession(userSession);
						}
						session.setAttribute("user", user2);
						return SUCCESS;
					}
				} else {
					this.setHint("failed");
					this.setTip("不存在這樣的Email或使用者名稱或密碼錯誤");
					return INPUT;
				}
			}
		}
	}
}

<p class="indent">


但是這樣檢視cookie資料夾,不生成cookie檔案,弄了一個星期了,真得弄不清楚到底是因為什麼傳送不成功,bang老師你可以指點一下嗎?

相關文章