struts2中add cookie 不能再客户端生成cookie文件,为什么?

我做自动登录的功能,是这样操作的:
[code="java"]
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;
            }
        }
    }
    

    }
    }

[/code]
奇怪的是,客户端就是不能生成cookie文件,我真的不知道是为什么了,哪位大哥分析一下是什么原因,小弟感激不尽

3个回答

没仔细看楼主的代码,你的domain是什么值? domain应该是xxxx.com的形式,比如网站是www.baidu.com,domain应该是baidu.com。

客户端是否把cookie关闭了?

没写成功。。这问题有很多种。。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐