You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Greg Lindholm <gr...@gmail.com> on 2014/02/06 15:14:44 UTC
S2 how to determine if a session exists from an Interceptor
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
Re: S2 how to determine if a session exists from an Interceptor
Posted by Lukasz Lenart <lu...@apache.org>.
http://struts.apache.org/release/2.3.x/docs/create-session-interceptor.html
2014-02-06 19:42 GMT+01:00 Dave Newton <da...@gmail.com>:
> 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 <gr...@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 <gr...@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>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: S2 how to determine if a session exists from an Interceptor
Posted by Dave Newton <da...@gmail.com>.
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 <gr...@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 <gr...@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>
Re: S2 how to determine if a session exists from an Interceptor
Posted by Greg Lindholm <gr...@gmail.com>.
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 <ke...@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 <gr...@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
> > >
> >
>
Re: S2 how to determine if a session exists from an Interceptor
Posted by Ken McWilliams <ke...@gmail.com>.
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 <mc...@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 <gr...@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
> >
>
Re: S2 how to determine if a session exists from an Interceptor
Posted by Maurizio Cucchiara <mc...@apache.org>.
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 <gr...@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
>