从jsf bean获取cookie时,它应该将我重定向到欢迎页面
但是在测试时我会收到以下错误:
页面未正确重定向
Firefox检测到服务器正在以永远无法完成的方式重定向此地址的请求。
This problem can sometimes be caused by disabling or refusing to accept cookies.
我的登录bean:
@ManagedBean
@SessionScoped
public class LoginBean implements Serializable {
private static final long serialVersionUID = 7765876811740798583L;
private String username;
private String password;
private boolean loggedIn;
String remember1 = "hi";
@ManagedProperty(value = "#{navigationBean}")
private NavigationBean navigationBean;
/**
* Login operation.
*
* @return
*/
public LoginBean() {
checkCookie();
}
@PostConstruct
public void init() throws IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
String cookieName = null;
Cookie cookie[] = ((HttpServletRequest) facesContext
.getExternalContext().getRequest()).getCookies();
if (cookie != null && cookie.length > 0) {
for (int i = 0; i < cookie.length; i++) {
cookieName = cookie[i].getName();
if (cookieName.equals("btuser")) {
username = cookie[i].getValue();
} else if (cookieName.equals("btpasswd")) {
password = cookie[i].getValue();
} else if (cookieName.equals("btremember")) {
remember1 = cookie[i].getValue();
if (remember1.equals("false")) {
loggedIn = false;
} else if (remember1.equals("true")) {
loggedIn = true;
}
}
if (loggedIn) {
ExternalContext externalContext = FacesContext
.getCurrentInstance().getExternalContext();
externalContext.redirect(externalContext
.getRequestContextPath()
+ "/secured/Main/AdminBoard.xhtml");
}
}
} else
System.out.println("Cannot find any cookie");
}
public String doLogin() {
try {
Dbconnection NewConnect = new Dbconnection();
Connection con = NewConnect.MakeConnect();
Statement stmt = con.createStatement();
ResultSet rs = stmt
.executeQuery("select school_username , school_pass from schools where school_username ='"
+ username
+ "'"
+ "and school_pass = '"
+ password
+ "'");
while (rs.next()) {
String dbUsername = rs.getString(1);
String dbPassword = rs.getString(2);
if (dbUsername.equals(username) && dbPassword.equals(password)) {
loggedIn = true;
// Save the userid and password in a cookie
Cookie btuser = new Cookie("btuser", username);
Cookie btpasswd = new Cookie("btpasswd", password);
if (loggedIn == false) {
this.remember1 = "false";
} else {
this.remember1 = "true";
}
Cookie btremember = new Cookie("btremember", this.remember1);
btuser.setMaxAge(3600);
btpasswd.setMaxAge(3600);
btremember.setMaxAge(3600);
FacesContext facesContext = FacesContext
.getCurrentInstance();
((HttpServletResponse) facesContext.getExternalContext()
.getResponse()).addCookie(btuser);
((HttpServletResponse) facesContext.getExternalContext()
.getResponse()).addCookie(btpasswd);
((HttpServletResponse) facesContext.getExternalContext()
.getResponse()).addCookie(btremember);
return navigationBean.redirectToWelcome();
}
}
rs.close();
stmt.close();
con.close();
} catch (Exception ex) {
}
// Set login ERROR
FacesMessage msg = new FacesMessage("خطأ!",
"اسم المستخدم او كلمة المرور خاطئة");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(null, msg);
// To to login page
return navigationBean.toLogin();
}
public void checkCookie() {
FacesContext facesContext = FacesContext.getCurrentInstance();
String cookieName = null;
Cookie cookie[] = ((HttpServletRequest) facesContext
.getExternalContext().getRequest()).getCookies();
if (cookie != null && cookie.length > 0) {
for (int i = 0; i < cookie.length; i++) {
cookieName = cookie[i].getName();
if (cookieName.equals("btuser")) {
username = cookie[i].getValue();
} else if (cookieName.equals("btpasswd")) {
password = cookie[i].getValue();
} else if (cookieName.equals("btremember")) {
remember1 = cookie[i].getValue();
if (remember1.equals("false")) {
loggedIn = false;
} else if (remember1.equals("true")) {
loggedIn = true;
}
}
}
} else
System.out.println("Cannot find any cookie");
}
public String doLogout() {
// Set the paremeter indicating that user is logged in to false
loggedIn = false;
// Set logout message
FacesMessage msg = new FacesMessage("Logout succesxs!", "INFO MSG");
msg.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage(null, msg);
return navigationBean.redirectToLogin();
}
// ------------------------------
// Getters & Setters
public String getUsername() {
if (loggedIn == false) {
username = "";
return username;
} else {
return username;
}
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
if (loggedIn == false) {
password = "";
return password;
} else {
return password;
}
}
public void setPassword(String password) {
this.password = password;
}
public boolean getLoggedIn() {
return loggedIn;
}
public void setLoggedIn(boolean loggedIn) {
this.loggedIn = loggedIn;
}
public void setNavigationBean(NavigationBean navigationBean) {
this.navigationBean = navigationBean;
}
}
HTML PAGE:
<f:metadata>
<f:event type="preRenderView" listener="#{loginBean.init}"/>
</f:metadata>