This week's book giveaways are in the Cloud and AI/ML forums. We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line! See this thread and this one for details.
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?
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!
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.
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