JSF保存我的cookie并获取空数组



我正在为构建一个简单的登录,其中包含用户名、密码和记住我选项。像所有网页一样,我想保存密码和用户名(或自动登录访问者(。我尝试过使用cookie,但当我尝试加载它们时:

FacesContext facesContext = FacesContext.getCurrentInstance();
Cookie cookie[] = ((HttpServletRequest) facesContext.getExternalContext().getRequest()).getCookies(); 

我得到一个空数组。我调试了很多次代码,但我仍然不知道为什么会得到一个空数组。每次启用"记住我"时,我都会保存我的cookie,代码为:

((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(user_cookie);
            ((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(password_cookie);
            ((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(remember_cookie); 

以下是我的bean的完整代码:

package es.web.login;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author Adrian Stefanovski <adrian@elektrosoft.com.mk>
 */
@ManagedBean(name = "visitor", eager = true)
@SessionScoped
public final class Visitor {
    private String city;
    private String username;
    private String password;
    private boolean remember;
    String remember_string = "false";
    public Visitor() {
        checkCookie();
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void setRemember(boolean remember) {
        this.remember = remember;
    }
    public boolean getRemember() {
        return remember;
    }
    public String login() {
        if (username.equals("dino") && password.equals("dino")) {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            Cookie user_cookie = new Cookie("user_cookie", username);
            Cookie password_cookie = new Cookie("password_cookie", password);
            if (remember == false) {
                remember_string = "false";
            } else {
                remember_string = "true";
            }
            Cookie remember_cookie = new Cookie("remember_cookie", remember_string);
            user_cookie.setMaxAge(3600);
            password_cookie.setMaxAge(3600);
            remember_cookie.setMaxAge(3600);
            if (remember) {
                user_cookie.setMaxAge(3600);
                password_cookie.setMaxAge(3600);
                remember_cookie.setMaxAge(3600);
            } else {
                user_cookie.setMaxAge(0);
                password_cookie.setMaxAge(0);
                remember_cookie.setMaxAge(0);
            }
            ((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(user_cookie);
            ((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(password_cookie);
            ((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(remember_cookie);
            return "logedin";
        } else {
            return "failure";
        }
    }
    public void checkCookie() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Cookie cookie[] = ((HttpServletRequest) facesContext.getExternalContext().getRequest()).getCookies();
        Map<String, Object> requestCookieMap = facesContext.getExternalContext().getRequestCookieMap();
        int length = requestCookieMap.size();
        String cookieName;
        if (cookie != null && cookie.length > 0) {
            for (Cookie cookie1 : cookie) {
                cookieName = cookie1.getName();
                switch (cookieName) {
                    case "user_cookie":
                        username = cookie1.getValue();
                        break;
                    case "password_cookie":
                        password = cookie1.getValue();
                        break;
                    case "remember_cookie":
                        remember_string = cookie1.getValue();
                        if (remember_string.equals("false")) {
                            remember = false;
                        } else if (remember_string.equals("true")) {
                            remember = true;
                        }
                        break;
                }
            }
        } else {
            //"Cannot find any cookie
        }
    }
}

希望有人能帮我。顺便说一句,这是我在jsf的第一天,我希望我没有犯明显的错误。还有人能告诉我我的浏览器(在这种情况下是谷歌chrome(是否可以阻止我的网络应用程序保存cookie吗?

请参阅如何在JSF中设置和获取cookie的答案。它包含CookieHelper类的代码,该类可以在一个应用程序中多次使用。这个类的目的是在一个与业务逻辑无关的类中单独的cookie相关代码。

相关内容

  • 没有找到相关文章