aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes scoping questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "scoping questions" Watch "scoping questions" New topic
Author

scoping questions

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 236
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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 236
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
Bartender

Joined: Apr 07, 2010
Posts: 1529
    
  22

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: scoping questions
 
Similar Threads
Tag scope attributes ?
Q from Hanu's mock test
How can I include jsp file which contains jsp and jstl tags in the custom tag?
Custom tags...
A Speculation