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.
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.
ReplyDeleteI 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.
ReplyDeleteSweet stuff dude! Had been stuck with this logout-then-back-then-reload thing for long, but not anymore!
ReplyDeletethanks and regards.
still the back button is not disabled
ReplyDeleteThis is the best explanation of login, logout and session management using interceptors I found on internet. Thanks.
ReplyDeleteCan you please explain how to use interceptors while uploading files, specially images?
ReplyDelete