File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Cattle Drive and the fly likes How long to java classes stay 'active' in a web app? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "How long to java classes stay Watch "How long to java classes stay New topic
Author

How long to java classes stay 'active' in a web app?

Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Hi all,

for want of better words, I need to know what I said in the subject bar. I am doing the servlet assignments here on javaranch and one of them has a web app that lets you continually add videos to a list and display them. If I have a java class in the background with an arraylist, will this class and its arraylist persist everytime I make a request and add to the list or will a new instance of it be created everytime I send a request with more videos for the list? Do you know what I am trying to say?

Thanks,
Alan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61318
    
  66

Alan Smith wrote:I need to know what I said in the subject bar.

In future posts, please do not defer to the subject. Please state your query in the post.

If I have a java class in the background with an arraylist, will this class and its arraylist persist

That greatly depends on what you mean by "in the background". What does that mean?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Bear Bibeault wrote:
That greatly depends on what you mean by "in the background". What does that mean?


I am using the MVC model. If I write a Java class to store information about a video, ie. title, lead actor, genre, etc. and then write a class that will contain a list of all these videos, will this class get reinstantiated every time I make a request ie. add new videos to the list. I want to have a web page to display all videos in the list and a page to add videos to the list. The web app is here Its hard to explain!

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61318
    
  66

Alan Smith wrote:will this class get reinstantiated every time I make a request

That's entirely up to you. You have control over when the class is created, and where it will be stored. So you're asking the wrong question.

What is it that you want to happen? You should be asking how to accomplish that.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2853
    
  11

I'm going to move this thread to our Cattle Drive forum, since it's directly about one of the Cattle Drive assignments. Plus we don't get much traffic over here, and I get lonely.

So, as Bear was saying, nothing magically persists in the background for you. However, Java servlet containers (like Orion, which we use for the assignments) can maintain a session on behalf of each user. When new requests come in, the servlet tries to match them up with an existing session. In your servlet, you can retrieve the session (from the request object) and store things in it. The next request handled by that servlet, or any other servlet, can retrieve the session and see what's stored in there. The request object also has user-settable attributes, so you can store objects as attributes and then if you forward the request to another servlet or JSP for handling, it can read the attributes. However, once the request has been processed, those attributes are effectively gone. The assignments use request attributes a lot for handling error messages and other output.

Your video list, in real life, would be stored in a database, with maybe a copy in a session, or in a managed cache to reduce slow database hits. I don't have the assignment official solution in front of me, but I believe what we do there is just store it in static member data of the Video servlet. That's completely unrealistic since it means multiple users of the application would be working from the same list, and we're not even making any attempt to synchronize access to it. However, these are supposed to be simple examples and so they take some shortcuts.
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Hi guys,

thanks for the replys. I managed to get do that assignment. The only problem I ended up running into was how to access array indexs in jstl loops, which I found out how to do by an old post Bear replied to in 2006! JSTL is great

Thanks,
Alan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61318
    
  66

 
GeeCON Prague 2014
 
subject: How long to java classes stay 'active' in a web app?