• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

scoping questions

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I've been brushing up on some reading regarding application, session, request, and page scope. The book I'm reading talks about the setAttribute() and getAttribute() methods for storing objects so that they can be retrieved later, but it doesn't explain for how long those objects are stored there. Take for instance the page scope as it relates to a custom tag. If there is a call to mypage.jsp which contains a custom tag and the class that extends TagSupport stores, lets say a HashMap using pageContext.setAttribute(AD, map, 4);, for how long will that HashMap remain stored there? If I refresh my browser on the same page "mypage.jsp", the class that extends TagSupport is able to retrieve the HashMap to work with it again. But let's say I now am finished with mypage.jsp and have navigated away to somewhere else. What happens to the HashMap that is stored in the pageContext? Will it get removed by the garbage collector after a certain period of time? Or does the pageContext know when the user is no longer accessing mypage.jsp, in which case it calls removeAttribute() on the HashMap after I'm gone?

I'm posing these questions because I'm concerned about new objects being stored by the servlet container that are really no longer required. Is there a danger that if we don't explicitly remove an object being stored that we'll eventually crash the server?

Alan
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If there is a call to mypage.jsp which contains a custom tag and the class that extends TagSupport stores, lets say a HashMap using pageContext.setAttribute(AD, map, 4);, for how long will that HashMap remain stored there? If I refresh my browser on the same page "mypage.jsp", the class that extends TagSupport is able to retrieve the HashMap to work with it again.


Is this a question or are you telling us you saw this behavior? Because PageContext.setAttribute sets attributes to page scope which is somewhat like request scope and shouldn't persist between multiple requests. You can find various resources/discussions on this:

http://www.java-samples.com/showtutorial.php?tutorialid=1009
https://coderanch.com/forums/posts/list/166495#808333
http://www.examulator.com/moodle/mod/resource/view.php?id=459
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't answer my question. Regardless of what scope I use. When an object is saved, let's say to a session, and the session finishes its life cycle, what happens to the object that was saved?
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alan,

Garbage collection in a web-container is not different from what you have learned in OCPJP. Generally speaking as soon as the Response gets committed the objects stored in the PageContext (and HttpServletRequest) are eligible for garbage collection.

You can get some of the information from the API

ServletRequest
void removeAttribute(java.lang.String name)
Removes an attribute from this request. This method is not generally needed as attributes only persist as long as the request is being handled.


The session object and application context are of course potentially dangerous as multiple threads can store objects in it, and developers should really take care of not blowing up the web-application.

Is there a danger that if we don't explicitly remove an object being stored that we'll eventually crash the server?


Yes and No. For the request and page scope you shouldn't worry as a programmer (the container will take care of it). For the session and application scope a programmer should really take care (so remove objects when they are not needed anymore)

Regards,
Frits
reply
    Bookmark Topic Watch Topic
  • New Topic