File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Which Scope to use in Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Which Scope to use in Bean" Watch "Which Scope to use in Bean" New topic
Author

Which Scope to use in Bean

Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
I'm working on an application that stores news articles in a database. When the servlet is intialized, it gets all the articles from the datatbase and stores them in a bean. The main page which is a jsp page, then displays the articles by using the bean. My question is what scope should I use for the bean? Ideally I'd like any updates in the database to be reflected in the bean. Could I designate a certain scope to do this, or am I going to have to have another call to the servlet?

Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Hai!
If u want to reflect the updations to database in a final JSP page then my suggestion is to call Servlet on each request and for this request scope for the bean is enough.
Or else u can set some thread in Servlet(which periodically reads databse)and updates bean. Then ur value bean can be of a application scope. But if ur following this approach ur sacrificing one thing..... that is displaying latest news. It all depends on time interval ur using to fetch data from database. But generally news Items won't change for every minute. So my suggestion is to go for say 30 min interval for data base retrieval.
Any Clarifications are loop holes in my suggestion are most welcome.
Rgds
Manohar
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Thanks for your reply.
I understand what you're saying. I've already created all the jsp pages, and the beans. I've also designed the database already. I haven't created the servlet that is going to control all the actions though. I have a question though.
The servlet is going to access the database and take all the necessary information and put it into a bean, and then the jsp page is going to use the bean to display the information. So when I start the application, all I need to do is intialize the servlet so the main jsp page will display information from the bean. (Let me know if you think I'm going about this the wrong way). Can I put the bean in the "application" scope, and have it created in the doPost method of the servlet. This way whenver someone enters something new to the database, the doPost method will also create a new bean that will get this new information? Would that hinder performance?
The jsp doesn't need to be updated every minute, but when a user submits new information, I'd like the user to be able to see that right away. Thanks for your help.

- Sean
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Hai Sean!
Even in my suggeted solution u need to call servlet each time. But what benifit we can get from making Bean Application scope is that u need not to access database and Instantiate Bean for every request.
But this decision again depends on your requirement. If u always want to display the latest news Then there is no other way but to access database and Instatiate bean on every request and for this request scope for the bean will be sufficient.(Donn use application scope if ur using this approach)

Second approach: Say i want to display the user with the news that is not older than 30 min. Then u can go by the approach that i suggested in my previous reply.
It's something like setting like our e-mail notifier.
if u wann to initimated as soon as u received a mail u set check interval as 1 second(First Approach...Checking DB always)
or else say i want to be notified for every 30 min if i have any new mail (Second approach ........Checking DB for every 30 min)
Again it depends on ur requirement.
Rgds
Manohar
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Thanks again for your help. I like the idea of keeping it in the request scope so it is always updated. In order for the main jsp page to display the information, the servlet needs to be initalized, so the bean will get initialized too. Here's my question:
Say I started the application with the bean in the request scope, and I initialized the servlet, so now the jsp page has information on it. Now if another user accesses the main jsp page, will they see the information that was there, or do they have to invoke the servlet themselves to see any information? This is where I'm a bit unsure.
Thanks again for your help.
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Hai!
When ever request is made to JSP ,Servlet will be invoked and whole process is repeated for every request(or for every user). What i got from ur queries is that ur following MVC architecture.
Visit
http://developer.java.sun.com/developer/onlineTraining/JSPIntro/contents.html#JSPIntro5(about scopes)
and if u scrolled bit down u will find discussion on MVC archi...

Still if u have doubts/Concerns related to this issue feel free to post again.
Rgds
Manohar
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Thanks again for all your help. Yes, I'm trying to use the MVC. From my understanding, and from the link you provided it looks like the controller servlet is the best point of entrance for the application. I was wondering if I had any alternatives to this and still use MVC. I was wondering if after initialization, could I use the main.jsp as a point of entrance? I don't see how I can since the controller servlet takes all requests, so maybe it would be better to have the servlet as the entrance? I'm not sure.
 
 
subject: Which Scope to use in Bean