Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6765685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:44:27+00:00 2026-05-26T14:44:27+00:00

While working with JSF in order to develop web applications using NetBeans, I many

  • 0

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 sometimes 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.

Here, I’m demonstrating a very simple application in which a getter method namely

public Collection<entity.Country> getCountries(){};

calls a remote EJB 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.

There is no need to pay much and special attention to the screen shot, JSF page code and it’s corresponding ManagedBean.

All countries from MySql database.

Here is the JSF page code

<?xml version='1.0' encoding='UTF-8' ?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">

<h:head>
    <title>Countries</title>
</h:head>

<h:body>
    <h:form>
        <center><br/><br/><br/>

        <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">

            <f:facet id="header" name="header">
                <h:outputText value="~:Country:~" styleClass="tableHeader"/>
            </f:facet>

            <h:column>
                <f:facet name="header">Country ID</f:facet>
                <h:outputText id="countryID" value="#{row.countryID}"/>
            </h:column>

            <h:column>
                <f:facet name="header">Country Name :</f:facet>
                <h:outputText id="countryName" value="#{row.countryName}"/>
            </h:column>

    </h:dataTable>
        </center>
    </h:form>
</h:body>
</html>

and the corresponding simple JSF ManagedBean code goes here.

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<entity.Country>countries=null;

    public Country() {}

    public Collection<entity.Country> 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<entity.Country> countries)
    {
        this.countries = countries;
    }

}

The dataTable in which the actual data are being displayed is bound to the countries property value="#{country.countries}" of the ManagedBean and it’s corresponding getter and setter are as follows.

public Collection<entity.Country> getCountries()
{
    countries=obj.getAllCountries();
    System.out.println("The Country() method called.");
    return countries;
}

public void setCountries(Collection<entity.Country> countries)
{
    this.countries = countries;
}

The method countries=obj.getAllCountries(); retrieves data from remote EJB. I felt that posting the actual implementation of this method (Which in EJB) here is quite unnecessary. All of these is just to show what I’m trying to.

Now, my actual question is that public Collection<entity.Country> getCountries(){} is being executed unnecessarily 8 times while it is bound to execute only once which is extremely critical in some very specific situations.

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, why…???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:44:28+00:00Added an answer on May 26, 2026 at 2:44 pm

    Long story short: Why JSF calls getters multiple times

    In this particular case, you should be using a @PostConstruct method instead to preload/initialize stuff retrieved from an injected business service, not a getter method.

    @ManagedBean
    @RequestScoped
    public class Country {
    
        @EJB
        private CommomBeanRemote commonBeanRemote;
        private Collection<entity.Country> countries;
    
        @PostConstruct
        public void init() {
            countries = commonBeanRemote.getAllCountries();
        }
    
        public Collection<entity.Country> getCountries() {
            return countries;
        }
    
        // Setter is completely unnecessary here.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While working on my code lately, I've noticed that some of the memory usage
While working with MySQL and some really performance greedy queries I noticed, that if
While working within C++ libraries, I've noticed that I am not granted any intellisense
while working with WF 4.0 I noticed that the WorkflowApplication class exposes action properties
While working with ASP.NET MVC, I have noticed that exception messages issued by the
While working with a Java class in Scala, I noticed that Scala can't multiply
While working between a Windows MySQL server and a Debian MySQL server, I noticed
While working a project tonight, I ended up using one .js resource file for
While working with ASP.NET using Visual Studio (2008) I have discomfort issue: source code
While working with some random sql queries on our databases, we may not want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.