开发者

struts 1.x form scope, why is request scope faster?

struts 1.x

I have always defined a struts form action mapping with scope="request" unless forced into session. An Example:

    <action
                path="/hello/my/oldfriend"
                type=开发者_如何学C"com.imFine.HowAreYouAction"
                name="greetingActionForm"
                scope="request"
                validate="true"
                input="/the/front/door">
                <forward
                    name="success"
                    path="/go/get/drinks.do" />
            </action>

If I convert this action mapping from request scope to session scope, then I almost always see a performance decrease. What are the extra method calls to the struts servlet that cause the extra workload for session scoped form beans?


There is indeed a difference in how the ActionForm is handled when request scoped or session scoped.

For request scope, when the user submits the HTML form, Struts instantiates your ActionForm, binds the request parameters onto it, then places it in request scope with a request.setAttribute(...) for the view to consume. Once the request has been handled, the ActionForm disappears (subject for garbage collection) because all data of the request is now out of scope. Each new request causes the ActionForm to be created, used and destroyed.

For session scope, when the user submits a form, Struts tries to find an ActionForm inside the session. If it finds one, it uses it and binds the request parameters onto it. If it does not find one it creates one and places it into session with session.setAttribute(...). Once the request has been handled the ActionForm remains in session and for further requests it is reused.

The above should not cause significant performance overhead.

A session means data on the server for each user of your application. This data means memory. More users means more memory. Servers usually move this data on persistent storage when there is more user data than memory can handle. The session data is serialized on disk when not needed and then deserialized when needed again (a database is another storage type for the data).

Maybe this is what happens. Not enough memory and the server stores/restores it on disk causing IO operations which are slower than memory access.

How Struts handles the forms depending on specified scope might be a red herring. Check your session first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜