bang老师 真的是需要你帮忙了,是有关Struts2 action中操作cookie

10-12-26 tianhandigeng
我做自动登录的功能,是这样操作的:

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>

但是这样查看cookie文件夹,不生成cookie文件,弄了一个星期了,真得弄不清楚到底是因为什么发送不成功,bang老师你可以指点一下吗?

猜你喜欢