4

It would be great if someone can help me understand, what's the advantage of ViewScoped bean when we have SessionBean injected into it.

Can we still save on session memory usage?

If we use only SessionScoped bean or ViewScoped bean with injected SessionBean, I believe there is no difference in session memory footprint.

Why do we go through so much hassles of using View and Session scoped beans when everything is achieved so smoothly with SessionScoped beans.

Thanks, Sundeep

Sundeep Kadakia
  • 71
  • 1
  • 2
  • 4
  • 3
    Smoothly? Open the same form in 2 or more different browser tabs in the same session. Enter values in one and other, switch tabs, etc and you'll see that the data get corrupted with data of other tabs. That's one of the reasons why view scope exist. The session scope should no longer be abused for view scoped data. See also http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope – BalusC Dec 09 '11 at 01:33

2 Answers2

11

It's a very common situation when you need to keep the data just for one page and then destroy it when you navigate to another page. That makes @ViewScoped bean a reasonable choice. @SessionScoped managed bean will keep all the data in session. So, why pollute your session map, when the data is no longer needed?

Also note, that the @ViewScoped annotation is not available in CDI. So if you're using beans with @Named annotation (rather than @ManagedBean), then you're out of luck. However, there are some alternatives.

jFrenetic
  • 5,384
  • 5
  • 42
  • 67
5

When you place everything in your session you are using more memory. The session usally expires after 35 minutes or your default.

Viewscoped beans are free for garbage collection after you switched the views.

For smaller objects it probably doesn't matter much in smaller applications. However if you for example store the return values from a database in your session you will have to care about the used up memory.

Udo Held
  • 12,314
  • 11
  • 67
  • 93