Nikolaus Rumm wrote:
Hello, sorry
if this message is struts-offtopic, but I have a very pragmatic question.I
find it very confusing setting and removing the variuos scoped attributes
by code. Are there any tools for planning a site, i.e. to illustrate which
action / xml-page sets or removes which attribute under which scope ?I
always have the problem that there might be some useless attributes left
in the current session/request if the site exceeds some size.Thx
for any answer. Nikolaus
Rumm
The way I tend to approach this kind of question is to review the
way that each scope's lifetime is managed, and then decide how long I need
to keep a particular object. It works like this:
* Page scope lasts only until the end of the current
JSP page, and are not visible to included pages.
The closest analog in servlet programming is a local
variable in your doGet() or doPost() method.
* Request scope lasts only until the end of the current
request, and are then thrown away. Such attributes
are visible to included pages. These are equivalent
to request attributes in servlet code.
* Session scope attributes last until you remove them,
until the session is invalidated, or until the session
times out. If you use session scope, you should remove
the attributes as soon as you no longer need to
keep them.
* Application scope attributes last until you remove them,
or until the application is shut down. These attributes
are generally initialized only when the application starts
up, and contain shared information. (For example, Struts
stores references to the applications' MessageResources
object here so that the custom tags on your JSP pages
can find them).
The basic rule I follow is this: use page scope or request scope
(depending on whether the attribute needs to be visible to included pages)
unless you need access to the object in a subsequent response and it is
not feasible to recalculate the object when the next request comes in.
Example cases where you do *not* need to use session scope:
* A single-page form that always submits all of the
fields on the form. Even if Struts discovers a
validation error and forwards control back to the
input form page again, the form bean will still
be there to recreate the page.
* An environment where you are storing state
information in a database or other storage area,
and reloading it on subsequent requests. This
is a common way to deal with a shopping cart,
so that the information is not lost if the server
goes down. Typically, the stored information will
be keyed by the session id, or by a value included
in a hidden field on every request.
A common case where you should store an ActionForm bean in the session,
on the other hand, is when you have a multi-page (wizard-style) input form.
But you should be sure to remove this attribute (session.removeAttribute("key"))
when you are through with it, to avoid unneeded garbage from accumulating.
Craig McClanahan
====================
See you at ApacheCon Europe <http://www.apachecon.com>!
Session VS01 (23-Oct 13h00-17h00): Sun Technical Briefing
Session T06 (24-Oct 14h00-15h00): Migrating Apache JServ
Applications to Tomcat
|