• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65126
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65126
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65126
92
IntelliJ IDE Java jQuery Mac Mac OS X
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic