struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dave Newton <davelnew...@gmail.com>
Subject Re: S2 how to determine if a session exists from an Interceptor
Date Thu, 06 Feb 2014 18:42:45 GMT
There's the session interceptor. It's unusual that you'd execute an action
and not have a session, though, since JSP pages will create their own if
one doesn't exist and it (the JSP) isn't explicitly configured not to
create one.


On Thu, Feb 6, 2014 at 1:33 PM, Greg Lindholm <greg.lindholm@gmail.com>wrote:

> My Interceptor is expecting a session to exist, but I'm occasionally
> getting exceptions that say the session doesn't exist, and since this
> occurs after the action has executed and response has been committed it is
> too late to create a session.
>
> So my question is; From with an Interceptor is there a way to determine if
> a session already exists (or doesn't exist)?
>
> I realize I could just catch the exception but I would rather not cause the
> exception if I can avoid it.
>
> The object returned from invocation.getInvocationContext().getSession() is
> not really a map, it is a wrapper around the session that implements a map
> interface. Using the put() method above will create the session if it
> doesn't exist (and response hasn't already been committed.)
>
> I suppose I could get the HttpServletRequest object and do a
> getSession(false) if there isn't a more "struts" way of finding out.
>
>
>
>
> On Thu, Feb 6, 2014 at 12:28 PM, Ken McWilliams <ken.mcwilliams@gmail.com
> >wrote:
>
> > The OP has abstracted away most of the details, he is working with
> > Map<String, Object>! The session is not generally struts2s responsibility
> > it, generally it is maintained by the web container. Tomcat/Glassfish,
> etc.
> > And generally you would consider it in such an environment to always
> exist.
> > What kind of container are you deploying your application in? I recall
> > there being an interceptor that will attempt to create a session for
> light
> > weight containers, however if struts needs to create such a session
> object
> > it is doubtful that the container will be able to use it in a meaningful
> > way.
> >
> >
> > On Thu, Feb 6, 2014 at 9:58 AM, Maurizio Cucchiara <
> mcucchiara@apache.org
> > >wrote:
> >
> > > Hi Greg,
> > > I seem to recall that this is not the best way to handle the session
> and
> > > generally speaking, it always better to abstract as much as possible
> > http's
> > > details
> > >
> > > I could be wrong, but It would be better if your action implements a
> > > specific interface (SessionAware or HttpSessionAware)
> > >
> > > Twitter     :http://www.twitter.com/m_cucchiara
> > > G+          :https://plus.google.com/107903711540963855921
> > > Linkedin    :http://www.linkedin.com/in/mauriziocucchiara
> > > VisualizeMe:
> http://vizualize.me/maurizio.cucchiara?r=maurizio.cucchiara
> > >
> > > Maurizio Cucchiara
> > >
> > >
> > > On 6 February 2014 14:14, Greg Lindholm <greg.lindholm@gmail.com>
> wrote:
> > >
> > > > I have an Interceptor that wants to put something in the session
> after
> > > the
> > > > action has executed.
> > > >
> > > > But if the session doesn't already exist I get an exception:
> > > >
> > > > java.lang.IllegalStateException: Cannot create a session after the
> > > response
> > > > has been committed
> > > > How can I determine if the session already exists from within an
> > > > Interceptor?
> > > >
> > > >  public String doIntercept(ActionInvocation invocation) throws
> > Exception
> > > >  {
> > > >    String result = invocation.invoke();
> > > >    Map<String, Object> session =
> > > > invocation.getInvocationContext().getSession();
> > > >    session.put(key, value); // throws exception if session doesn't
> > exist
> > > >    return result;
> > > >  }
> > > >
> > > > Thanks
> > > > Greg
> > > >
> > >
> >
>



-- 
e: davelnewton@gmail.com
m: 908-380-8699
s: davelnewton_skype
t: @dave_newton <https://twitter.com/dave_newton>
b: Bucky Bits <http://buckybits.blogspot.com/>
g: davelnewton <https://github.com/davelnewton>
so: Dave Newton <http://stackoverflow.com/users/438992/dave-newton>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message