aspose file tools*
The moose likes Struts and the fly likes Session expiration in struts using nested tags Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Session expiration in struts using nested tags" Watch "Session expiration in struts using nested tags" New topic
Author

Session expiration in struts using nested tags

Vivek Roy
Greenhorn

Joined: Jun 16, 2006
Posts: 11
Hi,
I might be the million'th person asking this but i really need some quick answer as I have been struggling with this for almost 5 days now..

My situation is :
I have a BasForm ,BaseAction and BaseDTO.
All my forms extend baseform, all actions extend baseactiona and all DTOs extend basedto.

In the forms I have data classes which I call DTO's. lets consider a scenario:
In a screen: I have displayDTO which extends baseDTO and this display DTO is in displayForm.

Now in baseaction I first check whether i have a dto already in the form other wise create a new dto and keep it in session.
This thing is done in baseaction before execute method of any action class is called. I retieve the dto from session and use that otherwise create a new.

This works fine as long as my session is valide. When session expires
the request comes to action servlet and then it tries to create a form before it actually calls the base action. Here is thows exception :
javax.servlet.ServletException: BeanUtils.populate

I know the problem is that the DTO property from the session has been removed. But i get to know this in form and then the control does not go to the action so that I can create a new DTO. Can somebody tell me what to do..why my control is not going to execute of baseaction..

Please help..
Thanks
Vivek
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

"VivekRanch Roy",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vivek Roy
Greenhorn

Joined: Jun 16, 2006
Posts: 11
I changed the display name...
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I guess this one ties into your other post and provides more detail....Right, Struts tries to populate your form before it calls your action. One of these might work 1) initialize your dto reference in your form's constructor, 2) initialize the dto reference in your form's reset method, or 3) initialized the dto reference in your form's getDto() method (or whatever your method is called...if(this.dto == null) this.dto = new displayDTO(); return this.dto

- Brent
Vivek Roy
Greenhorn

Joined: Jun 16, 2006
Posts: 11
Brent,
your prompt reply is making me very excited. I hope that I will find a solution.
Well, I can initialise the dto from my form's constructor or a reset method, but the problem is how will i know that my session has expired. I do not want to re initialize things once the session has expired.

What do you say

Thanks
-Vivek
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I am guessing that you have your action mapping configured to use session scope (or you are using the default scope, which is session). I don't see an issue initializing your reference in the form's constructor (that is if one type of form is always tied to one type of dto).

It also sounds like your jsp page must have fields named like "dto.name". During the population of the form, this translates into "form.getDto().setName(value)". The problem is that if getDto() returns null at this point then you get an error.

- Brent
Vivek Roy
Greenhorn

Joined: Jun 16, 2006
Posts: 11
Brent,
you are absolutely right.
My scope of action mapping is default. here is this:

<action path="/searchRules" type="com.nite.esm.clientxref.presentation.actions.RuleSearchAction" name="displaySearchForm">
<forward name="success" path="/jsp/displayRuleData.jsp"/>
<forward name="error" path="/jsp/error.jsp"/>
</action>

- I would initialize my dto in form reset method. but what i wanted to know is that how to know when your session expired so that you can throw the error out to the user.


Now keeping your suggestion in mind i initialize the dto in form.
I wait for some time to get the session invalidate.
now i go to action and try to retieve the session by:
HttpSession session = request.getSession(false);
ClientXrefBaseDTO baseDTOFromSession = (ClientXrefBaseDTO) session.getAttribute(Constants.BASE_DTO_IN_SESSION);
DisplaySearchDTO displaySearchDTO = (DisplaySearchDTO)baseDTOFromSession;

But the funny thing is that I do not get a null poniter exception which i should have....
I if I would have got one null pointer exeption I would have directed the users to some error page....

Here again I am getting a valid session variable...how is this possible??

-Vivek
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Sessions are not one of my strong suits and I am not sure how you can detect that a session has timed out. I will point out that the code that you posted would not produce an exception even if getAttribute returns null. If you tried to call a method on baseDTOFromSession or displaySearchDTO you would get a NullPointerException.

- Brent
Vivek Roy
Greenhorn

Joined: Jun 16, 2006
Posts: 11
I am determining session invalidation by implementating interaface HttpSessionBindingListener in the baseDTO. its method valueUnbound is called when that object is removed from the session. moreover at first in the base action i am setting session.setMaxInactiveInterval(5) so that session expires in 5 minutes.

One more thing, now when i initialize the dto in form reset method i get an error that some other nested property is null. so does that mean that i need to initalise each and every nested property in the dto..that would take much of effort ..don't you think so..
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
So when the user's session times out before the user clicks the submit button what do you plan on doing? If you are just going to send them to a "you session has timed out" page then maybe you need another approach. Maybe you could override methods in a custom RequestProcessor (or TilesRequestProcessor) class. You could at least override processPopulate to not populate the form if the session has timed out (though I am not exactly sure how you will tell that the session has timed out). There might be a better way. Maybe in processPreprocess?

- Brent
Vivek Roy
Greenhorn

Joined: Jun 16, 2006
Posts: 11
Hey Brent,
Absolutely great. Thanks for the RequestProcessor approach. I could get a handle of the flow before the form is getting populated.
Now I believe that I should be able to redirect user to a "session expiration" page.
Now my biggest problem is how to detect the session has timed out???
See, As i told you i have set the maximum inactive interval as 5 secs. So I do not do anything for 5 secs and I can see that the valueUnbound method of the classes which implement the HttpSessionBindingListener interface.
Now in the overridden implemenatation of processPreprocess I do somthing like:
HttpSession session = request.getSession(false);

But here when I check if (session == null)
I do not get a null session. rather it creates a new one after the old one got invalidated...
Can you tell me how to check the invalidity of the session...

Thanks a lot!!
-Vivek
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
A search on the web for a phrase like "struts detect session timeout" returns a number of hits. Here is one that looks like it has some decent ideas: http://www.theserverside.com/discussions/thread.tss?thread_id=21643

BTW, I don't completely understand the use of HttpSessionBindingListener. What are you supposed to do when valueUnbound is called? Seems like it might be useful for closing resources but I am not sure how to use this mechanism for detecting that a session has timed out.

- Brent
Vivek Roy
Greenhorn

Joined: Jun 16, 2006
Posts: 11
Breant,

That is what I did. I kept an attribute at the begenning and check that in the RequestProcessor.
I just used HttpSessionBindingListener just to see when my session is getting timed out by putting some logs in the valueUnbound method.

Anyways, I think my problem is solved now(though i need to do a good testing now) ...and it was all due to YOU!!!

Thank you brother for your time and suggestion..I will remember you all the time....I really appreciate the effort you put in to solve my problem...

Thanks once again..

have a nice day!!

-Vivek
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
You are welcome. Glad I could be of help.

- Brent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session expiration in struts using nested tags