Hello everybody :) I am implementing a dropdown autocomplete. This is my code so far:

public class MealBean {

    private String mealType;

    List<MealType> allMeals = DataHelper.getMealTypes();

    public List<String> complete(String query) {
        List<String> filteredMeals = new ArrayList<String>();

        for (int i = 0; i < allMeals.size(); i++) {
            MealType mType = allMeals.get(i);

            if (mType.getTypeOfMeal() != null
                    && mType.getTypeOfMeal().toLowerCase().startsWith(query)) {
        return filteredMeals;

    public String getMealType() {
        return mealType;

    public void setMealType(String mealType) {
        this.mealType = mealType;


The DataHelper actually is used for reading values from the database. The complete method is invoked each time user enters something. So the thing i am wondering is that: when user enters some character is the DataHelper.getMealType() method invoked,or just once when the bean is created, if so how I can optimize the code because it will be not a good practice to ask the database for result each type user enters something.

Thanks :)

asked 22.12.2016 at 08:53

tano9321's gravatar image

accept rate: 0%

A managed bean has always a scope and the scope defines how long a bean lives. The default scope is request. Means the bean gets created for each request at most once. So when the same bean is used multiple times in the same request, always the same beans instance is used. Therefore the method DataHelper.getMealType() is executed on each request once.

JSF 2.x supports the below scopes. Your choice will probably be @ViewScoped.

  • @SessionScoped
  • @RequestScoped
  • @ApplicationScoped
  • @ViewScoped

Session Scope: The session scope persists from the time that a session is established until session termination. A session terminates if the web application invokes the invalidate method on the HttpSession object, or if it times out.

RequestScope: The request scope is short-lived. It starts when an HTTP request is submitted and ends after the response is sent back to the client. If you place a managed bean into request scope, a new instance is created with each request. It is worth considering request scope if you are concerned about the cost of session scope storage.

ApplicationScope: The application scope persists for the entire duration of the web application. That scope is shared among all requests and all sessions. You place managed beans into the application scope if a single bean should be shared among all instances of a web application. The bean is constructed when it is first requested by any user of the application, and it stays alive until the web application is removed from the application server.

ViewScope: View scope was added in JSF 2.0. A bean in view scope persists while the same JSF page is redisplayed. (The JSF specification uses the term view for a JSF page.) As soon as the user navigates to a different page, the bean goes out of scope.

Source: Core Java Server Faces 3rd Edition by David Geary & Cay Horstmann [Page no. 51 - 54]):


answered 22.12.2016 at 09:56

Flavio%20Sadeghi's gravatar image

Flavio Sadeghi ♦♦
accept rate: 75%

thank you, that was the information I need :)

(22.12.2016 at 11:22) tano9321 tano9321's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 22.12.2016 at 08:53

Seen: 2,805 times

Last updated: 22.12.2016 at 11:22