title: flash.keep & flash.setKeepMessages(true) on redirection
body: <p>I am going through an example in <em>Anghel Leonard</em> book <em>Mastering Java Server Faces 2.2</em>.</p>
  
  <p>The author demonstrates with an example on how preserve data for the next request on redirection when the bean is made is <code>@RequestScoped</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/FxAUr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FxAUr.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/b5mmA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b5mmA.png" alt="enter image description here"></a></p>
  
  <p>This is the code for index.xhtml-</p>
  
  <pre><code>&lt;h:body&gt;
      &lt;f:metadata&gt; 
          &lt;f:event type="preRenderView" 
              listener="#{playersBean.pullValuesFromFlashAction}"/&gt; 
      &lt;/f:metadata&gt;
      &lt;h:messages /&gt;  
      &lt;h:form&gt;                       
          Name: &lt;h:inputText value="#{playersBean.playerName}"/&gt;
          Surname: &lt;h:inputText value="#{playersBean.playerSurname}"/&gt;
          &lt;h:commandButton value="Register" 
              action="#{playersBean.addValuesToFlashAction()}"/&gt;          
      &lt;/h:form&gt;
  &lt;/h:body&gt;
  </code></pre>
  
  <p>terms.xhtml-</p>
  
  <pre><code>&lt;h:body&gt;
      &lt;h:messages /&gt;  
      Hello, &lt;h:outputText value="#{flash.keep.playerName} #{flash.keep.playerSurname}"/&gt;    
      &lt;br/&gt;&lt;br/&gt;Terms &amp;amp; Conditions ... ... ... ... ...
      &lt;h:form&gt;
          &lt;h:commandButton value="Reject" 
              action="#{playersBean.termsRejectedAction()}" /&gt;
          &lt;h:commandButton value="Accept" 
              action="#{playersBean.termsAcceptedAction()}" /&gt;
      &lt;/h:form&gt;
  &lt;/h:body&gt;
  </code></pre>
  
  <p>done.xhtml-</p>
  
  <pre><code>&lt;h:head&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/h:head&gt;   
  &lt;h:body&gt;
      &lt;f:metadata&gt; 
          &lt;f:event type="preRenderView" 
              listener="#{playersBean.pullValuesFromFlashAction}"/&gt; 
      &lt;/f:metadata&gt;
      &lt;h:messages /&gt;  
      &lt;h:outputText value="#{playersBean.playerName} #{playersBean.playerSurname}"/&gt; 
          successfully registered!           
  &lt;/h:body&gt;
  </code></pre>
  
  <p>And the backing bean-</p>
  
  <pre><code>@ManagedBean
  @RequestScoped
  public class PlayersBean {
  
      private final static Logger logger = Logger.getLogger(PlayersBean.class.getName());
      private String playerName;
      private String playerSurname;
      public PlayersBean() {
      }
      public String getPlayerName() {
          return playerName;
      }
      public void setPlayerName(String playerName) {
          this.playerName = playerName;
      }
      public String getPlayerSurname() {
          return playerSurname;
      }
      public void setPlayerSurname(String playerSurname) {
          this.playerSurname = playerSurname;
      }
      public String addValuesToFlashAction() {
  
          Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
          flash.put("playerName", playerName);
          flash.put("playerSurname", playerSurname);
          return "terms?faces-redirect=true";
      }
      public void pullValuesFromFlashAction(ComponentSystemEvent e) {
          Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
          playerName = (String) flash.get("playerName");
          playerSurname = (String) flash.get("playerSurname");
      }
      public String termsAcceptedAction() {
          Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
  
          flash.setKeepMessages(true);
          pullValuesFromFlashAction(null);
  
          //do something with firstName, lastName 
          logger.log(Level.INFO, "First name: {0}", playerName);
          logger.log(Level.INFO, "Last name: {0}", playerSurname);
  
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Terms accepted and player registered!"));
          return "done?faces-redirect=true";
      }
      public String termsRejectedAction() {
          Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
  
          flash.setKeepMessages(true);
          pullValuesFromFlashAction(null);
  
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Terms rejected! Player not registered!"));
          return "index?faces-redirect=true";
      }
  }
  </code></pre>
  
  <p>I think there was no need for these 2 lines-</p>
  
  <pre><code>flash.setKeepMessages(true);
  pullValuesFromFlashAction(null);
  </code></pre>
  
  <p>as flash.keep on terms.xhtml page serves the same purpose.
  The author seems to be confused here.</p>
  
  <p>Or am I completely wrong &amp; they do definitely serves a purpose over here.</p>