Shawn Lohstroh wrote:

> Can we force a bean loaded by the useBean tag to reload or go away even
> though it has not gone out of scope?
>
> The useBean tag is nice because a lot of loading and caching is done for
> you, but there are times when I don't need the bean anymore and I want to
> force it to go away even though it has not yet gone out of scope.
>
> I know I can manage this myself buy placing these objects on the session and
> removing them when I am done with them, but I'd like the useBean tag to load
> the bean and I tell it to go away.
>

One way would be to use pageContext.removeAttribute() to do this.  Further details
are in the JSP spec and the associated API documentation
(javax.servlet.jsp.PageContext).

Another way, depending on which scope the bean is in, is to use the corresponding
servlet method in a scriptlet:
* Request:  ServletRequest.removeAttribute() -- Servlet API version 2.2 or later
only
* Session:  HttpSession.removeValue()
* Application:  ServletContext.removeAttribute()

In all cases, what is happening is that the reference to your bean (in the
appropriate context) is removed.  If this was the last reference to that bean from
anywhere in the JVM, then it becomes eligible for garbage collection -- otherwise,
it stays in the JVM's heap until the last reference is removed.

In practice, this will probably be most useful on beans with SESSION scope that
are needed for a particular set of functions in your application, and then are no
longer needed.

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to