You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Raghu Kanchustambham <kr...@gmail.com> on 2005/11/23 02:44:43 UTC

why cant the struts action class provide a getSession() api ?

Hi,
I believe supporting a getSession() call is useful in the Action class.

let me walk you through a situation:

public class MyAppBaseAction extends DispatchAction
{
     //some helper functions

     isAuthorized( arguments )
     {


     }

}


public class MyAction1 extends MyAppBaseAction
{

      create(....)
      {
              isAuthorized(...)

              //restOfCode....

       }

}



Now in the above scenario... isAuthorized necessarily needs to be sent a
request object as one of the parameters from which it can extract the
session and then extract the user object from the session. This is not a
great pain, but I think the code would look cleaner if I dont have to pass
the request/session object around. If I had a getSession function support
from the struts action class, then isAuthorized doesnt need to have an
argument like Session or Request.

What do you think?

Regards,
Raghu

Re: why cant the struts action class provide a getSession() api ?

Posted by Raghu Kanchustambham <kr...@gmail.com>.
Thanks Max and Craig for your responses.
I agree with the overheads of pooling a stateful "entity" probably doesn't
justify its inclusion.

I was not looking for an api which gives me a session object after taking
the request object as an input parameter which as Craig points out is too
simplistic.

However, lets go by the "Threadlocal trickery". It may be a little tricky..
but then the regular struts user doesnt have to worry about it. It can be
implemented under the hood .. unless someone tells me its again going to be
a performance overhead to do it under the hood.

Going back to the specific use case I was referring to... I do not like
passing the 'user' object around. The code is executing in the context of
the user and any function written in the action class should have access to
it. Note that I dont like passing around the request/session object either
(if it can be got through a call like "getSession()" or "getRequest()" ....
dont know if I am missing something somewhere... but should not this be
possible through ThreadLocal?

Regards,
Raghu


On 11/23/05, Craig McClanahan <cr...@apache.org> wrote:
>
> On 11/22/05, Max Cooper <ma...@maxcooper.com> wrote:
> >
> > A single instance of each Action class is used to service multiple
> > simultaneous requests (just like a Servlet). If you want the session,
> > you have to pass it around (or pass the request around, from which you
> > can navigate to the session).
> >
> > Having a no-argument getSession() method on Action would require a
> > unique Action instance for each request (or Action instance pooling, or
> > thread-local trickery).
>
>
> Max is correct ... however the alternative would be to implement the
> method
> like this:
>
>    protected HttpSession(HttpServletRequest request) {
>        return request.getSession();
>    }
>
> Of course, that's so simple that there is not much value add in providing
> a
> helper method for it.  Seems simpler just to call request.getSession()
> yourself.
>
> -Max
>
>
> Craig
>
>

Re: why cant the struts action class provide a getSession() api ?

Posted by Craig McClanahan <cr...@apache.org>.
On 11/22/05, Max Cooper <ma...@maxcooper.com> wrote:
>
> A single instance of each Action class is used to service multiple
> simultaneous requests (just like a Servlet). If you want the session,
> you have to pass it around (or pass the request around, from which you
> can navigate to the session).
>
> Having a no-argument getSession() method on Action would require a
> unique Action instance for each request (or Action instance pooling, or
> thread-local trickery).


Max is correct ... however the alternative would be to implement the method
like this:

    protected HttpSession(HttpServletRequest request) {
        return request.getSession();
    }

Of course, that's so simple that there is not much value add in providing a
helper method for it.  Seems simpler just to call request.getSession()
yourself.

-Max


Craig

Re: why cant the struts action class provide a getSession() api ?

Posted by Max Cooper <ma...@maxcooper.com>.
A single instance of each Action class is used to service multiple
simultaneous requests (just like a Servlet). If you want the session,
you have to pass it around (or pass the request around, from which you
can navigate to the session).

Having a no-argument getSession() method on Action would require a
unique Action instance for each request (or Action instance pooling, or
thread-local trickery).

-Max

On Wed, 2005-11-23 at 07:14 +0530, Raghu Kanchustambham wrote:
> Hi,
> I believe supporting a getSession() call is useful in the Action class.
> 
> let me walk you through a situation:
> 
> public class MyAppBaseAction extends DispatchAction
> {
>      //some helper functions
> 
>      isAuthorized( arguments )
>      {
> 
> 
>      }
> 
> }
> 
> 
> public class MyAction1 extends MyAppBaseAction
> {
> 
>       create(....)
>       {
>               isAuthorized(...)
> 
>               //restOfCode....
> 
>        }
> 
> }
> 
> 
> 
> Now in the above scenario... isAuthorized necessarily needs to be sent a
> request object as one of the parameters from which it can extract the
> session and then extract the user object from the session. This is not a
> great pain, but I think the code would look cleaner if I dont have to pass
> the request/session object around. If I had a getSession function support
> from the struts action class, then isAuthorized doesnt need to have an
> argument like Session or Request.
> 
> What do you think?
> 
> Regards,
> Raghu


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org