Jsf CookieHelper.setCookie不工作

Jsf CookieHelper.setCookie不工作,jsf,cookies,setcookie,Jsf,Cookies,Setcookie,我尝试在登录后设置cookie。我在这个网站上找到了一个例子。但有时是有效的,有时不是。如果我重新启动然后运行,它就不工作了。这是我的密码 public class CookieHelper { public static void setCookie(String name, String value, int expiry) { FacesContext facesContext = FacesContext.getCurrentInstance(); HttpServlet

我尝试在登录后设置cookie。我在这个网站上找到了一个例子。但有时是有效的,有时不是。如果我重新启动然后运行,它就不工作了。这是我的密码

public class CookieHelper {
public static void setCookie(String name, String value, int expiry) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
    Cookie cookie = null;
    Cookie[] userCookies = request.getCookies();
    if (userCookies != null && userCookies.length > 0 ) {
        for (int i = 0; i < userCookies.length; i++) {
            if (userCookies[i].getName().equals(name)) {
                cookie = userCookies[i];
                break;
            }
        }
    }
    if (cookie != null) {
        cookie.setValue(value);
    } else {
        cookie = new Cookie(name, value);
        cookie.setPath(request.getContextPath());
    }
    cookie.setMaxAge(expiry);
    HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
    response.addCookie(cookie);
}

public static Cookie getCookie(String name) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
    Cookie cookie = null;
    Cookie[] userCookies = request.getCookies();
    if (userCookies != null && userCookies.length > 0 ) {
        for (int i = 0; i < userCookies.length; i++) {
            if (userCookies[i].getName().equals(name)) {
                cookie = userCookies[i];
                return cookie;
            }
        }
    }
    return null;
}
公共类CookieHelper{
公共静态void setCookie(字符串名称、字符串值、int到期){
FacesContext FacesContext=FacesContext.getCurrentInstance();
HttpServletRequest=(HttpServletRequest)facesContext.getExternalContext().getRequest();
Cookie=null;
Cookie[]用户cookies=request.getCookies();
if(userCookies!=null&&userCookies.length>0){
for(int i=0;i0){
for(int i=0;i
}

这是我的bean登录

@ManagedBean(name="login", eager=true)
@RequestScoped
  public class Login {
private String message ="Enter username and password.";
private String username;
private String password;
private String host;
private LazyDataModel<Message> lazyModel;

public String findHost(){
    return username.substring(username.indexOf("@")+1, username.indexOf(".com"));
}

public String login(){
    if(username.equals("admin@admin") && password.equals("admin")){
        return "admin";
    } else{
        try {
            Properties props = new Properties();
            props.put("mail.smtp.starttls.enable","true");
            props.put("mail.smtp.auth", "true");
            Session session = Session.getInstance(props, null);
            Transport transport = session.getTransport("smtp");
            String SVhost = findHost();
            transport.connect(CheckTypeMail.checkSmtp(SVhost), 587, username, password);
            setHost(CheckTypeMail.checkSmtp(SVhost));        
            transport.close();

            CookieHelper.setCookie("user", username, 60*60*24);
            CookieHelper.setCookie("pass", password, 60*60*24);
            CookieHelper.setCookie("host", SVhost, 60*60*24);
            return "index";
         } 
         catch(AuthenticationFailedException e) {
               setMessage("Error email or pass");
               return "index";
         }
         catch(MessagingException e) {
               setMessage("Error email or pass");
               return "index";
         }
    }
} ...
@ManagedBean(name=“login”,eager=true)
@请求范围
公共类登录{
私有字符串消息=“输入用户名和密码。”;
私有字符串用户名;
私有字符串密码;
私有字符串主机;
私人LazyDataModel lazyModel;
公共字符串findHost(){
返回username.substring(username.indexOf(“@”)+1,username.indexOf(“.com”);
}
公共字符串登录(){
如果(username.equals)(“admin@admin“&&password.equals(“admin”)){
返回“admin”;
}否则{
试一试{
Properties props=新属性();
props.put(“mail.smtp.starttls.enable”、“true”);
props.put(“mail.smtp.auth”,“true”);
Session Session=Session.getInstance(props,null);
传输=session.getTransport(“smtp”);
字符串SVhost=findHost();
transport.connect(CheckTypeMail.checkSmtp(SVhost),587,用户名,密码);
setHost(CheckTypeMail.checkSmtp(SVhost));
transport.close();
setCookie(“用户”,用户名,60*60*24);
CookieHelper.setCookie(“通过”,密码,60*60*24);
CookieHelper.setCookie(“主机”,SVhost,60*60*24);
返回“索引”;
} 
捕获(身份验证失败异常e){
setMessage(“错误电子邮件或传递”);
返回“索引”;
}
捕获(消息异常e){
setMessage(“错误电子邮件或传递”);
返回“索引”;
}
}
} ...
这里是页面索引。登录后,我检查cookie以加载邮件

@ManagedBean(name="loadMail", eager=true)
@ViewScoped
 public class LoadMail {
private Properties pros;
private Session session;
private Store store;
private Folder folder;
private LazyDataModel<Message> lazyModel;

private Message selectedMgs;

@PostConstruct
public void init() {
    Message[] ini = loadAllMailByFolder("INBOX");
    Collections.reverse(Arrays.asList(ini));
    lazyModel = new LazyMessageDataModel(ini);
}

public void onRowSelect(SelectEvent event) throws MessagingException {
    FacesMessage msg = new FacesMessage("Message Selected", ((Message) event.getObject()).getSubject());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public Message[] loadAllMailByFolder(String folderName){
    String username = CookieHelper.getCookie("user").getValue();
    String password = CookieHelper.getCookie("pass").getValue();
    String hostname = CookieHelper.getCookie("host").getValue();

    Message[] mgs = null;
    this.pros = System.getProperties();
    this.pros.setProperty("mail.store.protocol", "imaps");

    this.session = Session.getDefaultInstance(pros);
    try {
        this.store = this.session.getStore("imaps");
        switch(hostname){
            case "gmail":
                this.store.connect("imap.googlemail.com", username, password);
                break;
            case "yahoo":
                this.store.connect("imap.mail.yahoo.com", username, password);
                break;
            default:
                this.store.connect("imap.googlemail.com", username, password);
                break;
        }
        this.folder = store.getFolder(folderName);
        this.folder.open(Folder.READ_ONLY);
        mgs = this.folder.getMessages();

    } catch (NoSuchProviderException ex) {
       ex.printStackTrace();
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    //lazyModel = new LazyMessageDataModel(mgs);
    return mgs;
}
@ManagedBean(name=“loadMail”,eager=true)
@视域
公共类邮件{
私人物业;
非公开会议;
私人店铺;
私人文件夹;
私人LazyDataModel lazyModel;
私人信息选择;
@施工后
公共void init(){
Message[]ini=loadAllMailByFolder(“收件箱”);
Collections.reverse(Arrays.asList(ini));
lazyModel=新的LazyMessageDataModel(ini);
}
public void onRowSelect(SelectEvent事件)引发MessaginException{
FacesMessage msg=新的FacesMessage(“选择的消息”((消息)event.getObject()).getSubject());
FacesContext.getCurrentInstance().addMessage(null,msg);
}
公共消息[]loadAllMailByFolder(字符串folderName){
字符串username=CookieHelper.getCookie(“用户”).getValue();
字符串password=CookieHelper.getCookie(“pass”).getValue();
字符串hostname=CookieHelper.getCookie(“主机”).getValue();
消息[]mgs=null;
this.pros=System.getProperties();
this.pros.setProperty(“mail.store.protocol”、“imaps”);
this.session=session.getDefaultInstance(pros);
试一试{
this.store=this.session.getStore(“imaps”);
交换机(主机名){
案例“gmail”:
this.store.connect(“imap.googlemail.com”、用户名、密码);
打破
案例“雅虎”:
this.store.connect(“imap.mail.yahoo.com”,用户名、密码);
打破
违约:
this.store.connect(“imap.googlemail.com”、用户名、密码);
打破
}
this.folder=store.getFolder(folderName);
打开此文件夹(文件夹只读);
mgs=this.folder.getMessages();
}捕获(无此提供异常例外){
例如printStackTrace();
}捕获(消息例外){
例如printStackTrace();
}
//lazyModel=新的LazyMessageDataModel(mgs);
返回mgs;
}

有时它工作是因为它已经存在cookie。但当我关闭并重新启动计算机时,它不工作,但它与另一台计算机一起工作有时它工作是因为它已经存在cookie。但当我关闭并重新启动计算机时,它不工作,但与另一台计算机一起工作