File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes How to Check Session in Controller Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to Check Session in Controller" Watch "How to Check Session in Controller" New topic

How to Check Session in Controller

Satish Dasari

Joined: Dec 10, 2008
Posts: 2
Hi all,

I'm new to struts framework. I'm using struts 1.2.9.
In My application I have many controllers . When the user is logged in I'm setting an session attribute like session.setAttribute("user","true"). And when User logged out I'm removing the attribute from the session.

Here My Problem is.. I want to check the user existence in session for every action. I can check the user in every action writing the code in every method in every Controller, but there are so many Controllers and so many methods. Its difficult to check in every action. Is there any way to check the user in every action before the action is executed and forward the page to some other page. So I need some guidance to check the user in a common class before the action is executed.

Thanks in advance

Satish Raj
David Curry

Joined: Jun 11, 2009
Posts: 5

One way to achieve this is through inheritance and overriding. Every struts controller uses the execute() method to perform its duties. You can subclass struts Action, placing all common behavior in the execute() method and having all of your controllers subclass from this one class. The only thing about this is that each controller has its own mapping.findForward() declared in the execute() method - to make this flexible enough for all actions to use we must create an abstract method that will be used in all your controllers.... therefore, your code would have to have all controllers extend the one common class, and change the execute() method to call the abstract method.

It can look something like this:

The BaseAction class is the class all your controllers will extend:

Here is an example of one of your existing controller classes:

It will require your code to change, but once implemented you can place any common code for all controller actions in the BaseAction class without anything changing.

Things to note:
1. This will work on an Action level. Meaning that if the user should be logged in to perform the called action, it will test it.
1. ALL your current controller classes will have to extend BaseAction instead of Action.
2. ALL your current controller classes will have to change the execute() method name to performExecute()
3. If there is a controller that does not require a person to be logged you must override the isLoginRequired() method.

Hope this helps. Let me know how it goes!

David Newton

Joined: Sep 29, 2008
Posts: 12617

Or just use a filter.

Checking the session for null, btw, would require all JSPs to turn off automatic session creation.
David Curry

Joined: Jun 11, 2009
Posts: 5

Good point regarding the "session == null"; I threw the example together pretty quick.

I actually haven't used Filters; I'm googling it now.
Kousik Majumder
Ranch Hand

Joined: Sep 30, 2007
Posts: 237

Can somebody tell me what will be the entry in struts-config.xml for BaseAction.

Actually I have forwarded to error page from BaseAction through struts-config.xml .But it is not happening. Can not understand how the path will be called for BaseAction.

Please help.


Thanks in Advance,
I agree. Here's the link:
subject: How to Check Session in Controller
jQuery in Action, 3rd edition