top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Struts2: How to determine if a session exists from an Interceptor

+7 votes
323 views

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 session = invocation.getInvocationContext().getSession();
 session.put(key, value); // throws exception if session doesn't exist
 return result;
 }
posted Feb 6, 2014 by Garima Jain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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)

answer Feb 6, 2014 by Sanketi Garg
Similar Questions
+1 vote

We are in the beginning of the migration to struts2 and it seems like there will be some period when both frameworks will be active at the same time..the only problem now is accessing the session beans managed by struts2 in struts1 and vice-versa form beans from strut1 mapped in struts2 . Half of the jsp would use struts1 tags and other half struts2 tags. So there should be beans instantiated in both frameworks.

Probably struts2 bean can be injected to struts1 action with struts1 form still accessible as execute() argument, but what about other way? accessing struts1 form bean (not just data, but managed bean) in struts2 execute() ?

+1 vote

In struts2 , defined an interceptor, it triggered by Action, my question is

"inside the interceptor class, what is the function call that can tell caller's name, because that interceptor could be triggered by different Action, we need to log the caller's name, but how?"

+1 vote

Will it restrict the information being stored at the browser cache.

+1 vote

Just after updating struts to 2.3.15.2, all of ours applications stop working. Some of ours applications uses struts-convention-plugin, so only url can be used to access action's methods.

We are using a lot of url with "!input" methods, especially to manage form input and form validation.

"S2-019 - Dynamic Method Invocation disabled by default", seems to be a big security issue. So, is it safe to re-enable back DMI to true ? If not, how is it possible to not use DMI ?

+1 vote

I am migrating an application from Struts 1 to Struts 2 and frequently I am facing scenarios where I need to use request.getSession to set/get attributes. As far as my knowledge goes, this is not considered to be a best practice in Struts 2. What is the best was way to handle this scenario?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...