我正在使用用户名,密码构建一个简单的登录,并记住我的选项。像所有网页一样,我想保存密码和用户名(或自动登录访问者)。我尝试使用cookies,但当我尝试加载它们时:
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的第一天,我希望我没有做出明显的错误。也有人可以告诉我,如果我的浏览器(在这种情况下是谷歌浏览器)可以阻止我的网络应用程序保存cookie?
答案 0 :(得分:1)
请参阅answer如何在JSF中设置和获取Cookie。它包含CookieHelper类的代码,可以在一个应用程序中多次使用。此类的目的是在一个类中与业务逻辑无关的单独的cookie相关代码。