Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to Check Session in Controller

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

Satish Raj
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!

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

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

I actually haven't used Filters; I'm googling it now.
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic