title: How to Redirect with primefaces polling?
body: <p>I'm developing jsf with primefaces 5.0, I need to redirect user to another page if condition is satisfied in the backing bean. So I use <code>p:poll</code> to let the page check the condition every second, if the condition is met, redirect user to another page.</p>
  
  <p>I create a small project to try this scenario, which using poll to check whether <code>int cnt</code> is >= 10, if so, redirect user to <code>welcomePrimefaces.xhtml</code>. Also, there's another thread <code>T1</code> which is used to simply increase <code>cnt</code> so that the condition may satisfied some time.</p>
  
  <p>Here's what I've done:<br>
  <strong>Page:</strong>(Nothing special, just poll every second to check the status)  </p>
  
  <pre><code>&lt;h:form&gt;
      Hello from Facelets
      &lt;br /&gt;
      &lt;p:poll interval="1" listener="#{mainBean.checkStatus()}"/&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p><strong>Backing Bean:</strong>    </p>
  
  <pre><code>public class MainBean {
  
      private Integer cnt = 0;
  
      @PostConstruct
      public void init() {
          Thread t1 = new Thread(new T1(), "test");
          t1.start();
  
      }
  
      public void checkStatus() {
          synchronized (cnt) {
              System.out.println("cnt:" + cnt);
              if (cnt &gt;= 10) {
                  try {
                      Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
                      Object response = FacesContext.getCurrentInstance().getExternalContext().getResponse();
                      HttpServletRequest httpRequest = (HttpServletRequest) request;
                      HttpServletResponse httpResponse = (HttpServletResponse) response;
                      httpResponse.sendRedirect(httpRequest.getContextPath()
                              + "/welcomePrimefaces.xhtml");
                  } catch (IOException ex) {
                      System.out.println("Error!");
                  }
              }
          }
      }
  
      private class T1 implements Runnable {
  
          @Override
          public void run() {
              try {
                  while (true) {
                      Thread.sleep(1000);
                      synchronized (cnt) {
                          cnt++;
                      }
                  }
              } catch (InterruptedException ex) {
                  System.out.println("Thread Error!");
              }
          }
      }
  }
  </code></pre>
  
  <p>The result is the <code>cnt</code> being printed 10 times(from 0 to 9), and after that, it is stocked, not even the polling executed.<br>
  So what's wrong with my code? Please help me.<br>
  Thanks in advance.</p>