Session management using login interceptor in Struts 2.

Struts2Logo

In this application (MyLoginAPI) , i am maintaining session using interceptor.
As you all know, when you call an action, a stack of interceptor executes every time before and after the action.Here i  will create  a custom   interceptor,  login interceptor  ,for the session management.
Here is the scenario:
1. We will set a session in Action class.
2. Create an LoginInterceptor class, and check user exit in session or not.
3. If user exits in the session then call an action called by the user.
4. If user try to access direct url of   home page and if user does not exit in the session then will redirect to login page.
5. If session time out  then redirect to login page.
6. This interceptor will execute before every action except the loginUser action (so we  will not apply this interceptor to loginUser action ).
7. I put home.jsp in the WEB-INF/jsps/user/home.sp , so no one can access directly using directory path.
Here is the Directory Structure:




Login.jsp

Web.xml
Set welcome page as login page.

Struts.xml
Set all configuration here. Here i am creating new stack ,loginStack, which refers to default stack and loginInterceptor.I am applying this loginStack  interceptor satck to  homeAction action.Also defining global results  when authentication failed  it will redirect to login page.

LoginAction.java

LoginInterceptor.java
custom interceptor class for checking user in the session.

 home.jsp


Create this application according to directory structure and run this.Here are the screen shots:


If you will enter wrong username or password then it will show actionError message.

if you will enter username as jkit and password as jkit123 it will redirect to home page.

If you will click on LogOut link it will go back to login page with actionMessage.

 Download Source Code

Click here to download complete source code


Thank you for read this tutorial. See you in next tutorial.
Share on Google Plus

About JK STACK

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

6 comments:

  1. I just want to tell you that I am newbie to blogging and absolutely enjoyed this blog. More than likely I’m going to bookmark your site . You certainly have good stories. Thank you for sharing your web page.

    ReplyDelete
  2. I simply want to mention I am beginner to blogging and absolutely enjoyed this web page. Probably I’m going to bookmark your blog post . You really come with wonderful stories. Thanks for sharing with us your web page.

    ReplyDelete
  3. Sweet stuff dude! Had been stuck with this logout-then-back-then-reload thing for long, but not anymore!

    thanks and regards.

    ReplyDelete
  4. still the back button is not disabled

    ReplyDelete
  5. This is the best explanation of login, logout and session management using interceptors I found on internet. Thanks.

    ReplyDelete
  6. Can you please explain how to use interceptors while uploading files, specially images?

    ReplyDelete