Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts 2 - Interceptor V/S Action for checking login...

 
Sunil Kumar Pissaye
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

This is more of a design question.. I am new to struts 2 and exploring different possibilities...
I have an application in which some of my JSP pages and actions need to have the user logged in...
The accepted way i see to do this is ... Login Interceptor - Put the interceptor on each action you want behind a login...

But, using an interceptor I cannot control access to my JSP's and have to add some code in the JSP to check if the user is logged in or not...
I thought the best way to do this would be to add <s:action name="checkLogin" /> as the very first line in the JSP.. this would call the action and re-direct if the user is not logged in... OR put the user object in session and check for each JSP check if the user object exists...

Is this the best way to go? and what is the point of creating an action class and an interceptor? Cant we create one piece of code which can check for both JSP's and Actions ?
Why cant an interceptor intercept a JSP ?
Pardon me if my questions seem naive...

Thank you.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't put the login check in the JSP, you put the login check in the interceptor.

An action does the logging in--typically the action would put a user object into the session. The interceptor checks the session for a user object. If one exists, there's a user logged in.

JSPs should be under WEB-INF, which makes them inaccessible via direct browser addresses.
 
Sunil Kumar Pissaye
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

I just tried the solution you suggested... works like a charm

This is a much cleaner design then what I was looking at... thanks a lot
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
" Sunil Kumar PV " please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic