• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scoping questions

 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • 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
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
http://www.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
  • 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?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2531
112
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic