Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Check Session in Controller

 
Satish Dasari
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

regrds
Satish Raj
 
David Curry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satish,

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!

~DC
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just use a filter.

Checking the session for null, btw, would require all JSPs to turn off automatic session creation.
 
David Curry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,

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
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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,
Kousik
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic