This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes Should I use Filters for this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Should I use Filters for this?" Watch "Should I use Filters for this?" New topic

Should I use Filters for this?

Andrew McMillan

Joined: Nov 04, 2008
Posts: 4

Hope I get some responses to this question.

Our system uses Struts 1.2. It deals with insurance applications. Let me give you a brief description of an issue we are facing.

We have a requirement to only allow one user to be able to modify an insurance application at a given time - hence if a user A opens application 123, then user B cannot modify it. So user A locks the application for a given time. If this user does not perform any actions (i.e. leaves the application idle) for more than 20 minutes, then we must release the lock (times out) and allow other users to open and modify the application.

We have created a DB table to hold details of the locked applications and their associated users. Every time a user performs an action on these applications, we update a timestamp in this table so that the lock does not time out. So basically any action on the application will require a database call to check the validity (and if necessary) update the lock record.

So, I was wondering - could I use filters to basically intercept a set of actions - so that we look-up/update this database table and then delegate control to the associated controller/action classes?

Or am I better off having a base action class that basically does the above work and then have all the other relevant action classes extend this base class?

If any of the above is not clear, please let me know. I'm also open to any other suggestions/advice. Thanks very much!

Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
The Struts Action classes are part of the Controller, in MVC terms. The requirements that you describe are part of the application (in MVC terms, the Model).

The requirement of checking to see if something can be done or not should go in the application (Model), especially if it deals with checking a database table. Business logic should not be coded in the Struts Action classes or any of the filters associated with the HttpRequest.

Check out the Business Delegate design pattern for more information.

Create a "lock utility" class that will monitor the users behavior and check the database table. A Business Delegate can use this class to regulate the application.
[ November 09, 2008: Message edited by: James Clark ]
Andrew McMillan

Joined: Nov 04, 2008
Posts: 4
Hi James - thanks for the reply.

Yes, I am aware that we shouldn't be making database calls from the controller/filter. I will simply be looking up the relevant locking service/lock utility from within my action class - it is the service that will then check the table and make any updates, etc.

Since I will be monitoring a number of user actions, I was wondering what the best strategy would be to check what action/link they clicked on so as to then call my lock service. i.e. if filters would be appropriate or simply creating a base class whereby any of its children will then have to check the database (via the service).

Hope that makes sense.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
I think the best bet would be to create a Base Action class and then have all the relevant Action classes of the the application extend this Base Action class.

You could then add a private method in the Base Action class which will connect to the service. And you could then call this method from whichever Action you want

The Base Action will extend the Struts Action class.
I agree. Here's the link:
subject: Should I use Filters for this?
Similar Threads
IBM Exam 157 - Questions and Answers (Sample Exam)
Looking for web application design input.
Design Questions
Locking in SCJD Beta
GUI display & booking questions