Hi, I want to know which scope is better for beans in jsp pages that I'm creating. I have a bean which I'm currently giving a page scope. I'm using this bean in 4 jsp pages,one after the other,giving it a page scope in each page. What will be the benefit if I give it a session scope,if any ? Page scope allows the bean to exist only for that page, but session will allow the bean to exist till the end of a client's session. What are the advantages/disadvantages of having a bean with session scope ? Any clarification will be helpful. Thanks.
Tony Alicea
Desperado
Sheriff
Joined: Jan 30, 2000
Posts: 3219
posted
0
You skipped REQUEST scope... For any reason?
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
Page scope means that (1) if there is any state to maintain, you will have to either maintain it somewhere outside the bean, or to pass the state in the HTTP request. (2) you have the creation and garbage collection overhead for every page. (3) your memory requirements scale gently as the load increases. Session scope means that (1) the bean state will be retained automatically. (2) the object will be created when you first need it, and garbage collected only after you decide you're done with it or when the session expires. (3) memory requirements (for this bean) increase with the load, especially if you don't actively un-bind the bean from the session when you're done. Request scope - the difference between this and page scope becomes evident only when you're using forward or (dynamic) include. In your case, its properties are similar to page scope though. Generally, if you don't have any state in your bean at all, use application scope. If you don't have state to maintain between requests, use page/request scope. If you do have state to maintain and scalability isn't an issue, use session scope. If you do have state to maintain and scalability is an issue, things aren't as black and white as all that and you will have to make judicious choices between all the methods at your disposal: database persistence of state, session EJBs, servlet session-scoped beans, cookies, and passing state in the HTTP request. - Peter