title: Why are JSF ManagedBean methods executed more than once while they are intended to be executed only once?
body: <p>While working with JSF in order to develop web applications using NetBeans, I many a times noticed that in some circumstances, the getter methods (and probably setters too) in JSF ManagedBeand are executed more than once while they are intended to be executed only once. In such a scenario, it is <strong><em>sometimes</em></strong> extremely crucial to enforce some critical conditions (if etc.) for some operations (especially some calculations) to prevent them from being  overwhelmed. I had been keeping on trying to know the actual reason behind it but I could not.</p>
  
  <p>Here, I'm demonstrating a very simple application in which a getter method namely </p>
  
  <pre><code>public Collection&lt;entity.Country&gt; getCountries(){};
  </code></pre>
  
  <p>calls a remote <strong>EJB</strong> and retrieves all countries from a relevant table in MySql database and displays on JSF page. A screen shot of the web page is simply looks as below.</p>
  
  <p><strong><em>There is no need to pay much and special attention to the screen shot, JSF page code and it's corresponding ManagedBean.</em></strong></p>
  
  <p><img src="https://i.stack.imgur.com/Sudz2.png" alt="All countries from MySql database."></p>
  
  <p><em>Here is the JSF page code</em></p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"&gt;
  
  &lt;h:head&gt;
      &lt;title&gt;Countries&lt;/title&gt;
  &lt;/h:head&gt;
  
  &lt;h:body&gt;
      &lt;h:form&gt;
          &lt;center&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;
  
          &lt;h:dataTable id="dataTable" styleClass="table" frame="box" value="#
               {country.countries}" var="row" bgcolor="lightyellow" border="7" 
               cellpadding="7" cellspacing="7" rules="all" width="50%" dir="ltr"&gt;
  
              &lt;f:facet id="header" name="header"&gt;
                  &lt;h:outputText value="~:Country:~" styleClass="tableHeader"/&gt;
              &lt;/f:facet&gt;
  
              &lt;h:column&gt;
                  &lt;f:facet name="header"&gt;Country ID&lt;/f:facet&gt;
                  &lt;h:outputText id="countryID" value="#{row.countryID}"/&gt;
              &lt;/h:column&gt;
  
              &lt;h:column&gt;
                  &lt;f:facet name="header"&gt;Country Name :&lt;/f:facet&gt;
                  &lt;h:outputText id="countryName" value="#{row.countryName}"/&gt;
              &lt;/h:column&gt;
  
      &lt;/h:dataTable&gt;
          &lt;/center&gt;
      &lt;/h:form&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><em>and the corresponding simple JSF ManagedBean code goes here.</em></p>
  
  <pre><code>package country;
  
  import commonBean.CommomBeanRemote;
  import java.util.Collection;
  import javax.ejb.EJB;
  import javax.faces.bean.ManagedBean; 
  import javax.faces.bean.RequestScoped;
  
  @ManagedBean
  @RequestScoped
  
  public class Country
  {
      @EJB
      private CommomBeanRemote obj=null;
      private Collection&lt;entity.Country&gt;countries=null;
  
      public Country() {}
  
      public Collection&lt;entity.Country&gt; getCountries()
      {
          countries=obj.getAllCountries(); //Calls a remote EJB to retrieve the data.
          System.out.println("The Country() method called.");//Here this displays 8 times unnecessarily
          return countries;
      }
  
      public void setCountries(Collection&lt;entity.Country&gt; countries)
      {
          this.countries = countries;
      }
  
  }
  </code></pre>
  
  <p>The dataTable in which the actual data are being displayed is bound to the <strong>countries</strong> property <code>value="#{country.countries}"</code> of the ManagedBean and it's corresponding getter and setter are as follows.</p>
  
  <pre><code>public Collection&lt;entity.Country&gt; getCountries()
  {
      countries=obj.getAllCountries();
      System.out.println("The Country() method called.");
      return countries;
  }
  
  public void setCountries(Collection&lt;entity.Country&gt; countries)
  {
      this.countries = countries;
  }
  </code></pre>
  
  <p>The method <code>countries=obj.getAllCountries();</code> retrieves data from remote <strong>EJB</strong>. <em>I felt that posting the actual implementation of this method (Which in EJB) here is quite unnecessary.</em> All of these is just to show what I'm trying to.</p>
  
  <p>Now, my actual question is that <code>public Collection&lt;entity.Country&gt; getCountries(){}</code> is being executed unnecessarily 8 times while it is bound to execute only once which is extremely critical in some very specific situations.</p>
  
  <p>I have tried this so many times while increasing and decreasing the number of rows being displayed still this method is always executed 8 times, <strong>why...???</strong></p>