Login/Logout With Session

In this section, we are going to develop a login/logout application with session.

Ads

Tutorials   
Login form in Struts2 version 2.3.16 Struts 2 Login Application Struts 2 Hello World Annotation Example Struts 2 Tutorial: Easy steps to learn Struts 2 Struts2 Application in Eclipse: Running the application in Eclipse IDE Struts 2 Hello World Application using Eclipse Interceptors in Struts 2 Struts 2 Action Tag Struts 2 in Agile Development Environment Struts 2 UI Tags Generic Tags Creating own Interceptor What are Interceptors in Struts 2 and how do they work? Results Value Stack / OGNL Action in Struts 2 Framework Struts 2 Interceptors Struts 2.3.15.1 Maven - How to use Maven dependency of Struts 2.3.15.1? Download Struts 2.3.15.1 How Struts 2 Framework works? Architecture of Struts 2 Framework Features and Enhancements in Struts 2.3.15.1 Struts 2 Tutorials - Struts version 2.3.15.1 Struts Flow Diagram Step By Step Struts Tutorial Struts 2 Login Form Example Struts 2 Hello World Example Introduction to Struts 2 Framework Downloading and Installing Struts 2.3.8 Struts 2.3.8 released with new features Struts 2.3.8 Tutorials and Examples Subset Tag Actionerror Tag Struts 2 Autocompleter,Struts 2 Autocomplete Generator Tag Struts 2 Application Struts 2 zero configuration,Struts 2 zero configuration Example Static Parameter, Static Parameter in Struts 2, Struts 2 Static Parameter Pagination Example Struts 2.2.1 Title Struts2 ElseIf Tag Example Struts2 Else Tag Example Struts2 If,Else,ElseIf Tag Example Struts2 tag refrences and Example How to download, compile and test the tutorials using ant. Struts 2.1.8 Login Form Struts 2.1.8 Hello World Example Download Struts 2.1.8, Downloading and installing Struts 2.1.8 Showcase application
Ads

Login/Logout With Session

     

In this section, we are going to develop a login/logout application with session. This application checks the user authentication. Whenever you run, it takes a user id and a password (Both the user id and password is "admin") it displays the welcome page, when both fields are correctly filled.

 

Create an action mapping in the struts.xml file. Here is the code to be added in the struts.xml:

 

<action name="login" class="net.roseindia.loginAction" >
  <result name="success" type="dispatcher">/pages/uiTags/Success.jsp</result>
   <result name="error" type="redirect">/pages/uiTags/Login.jsp</result>
</action>

<action name="logout" class="net.roseindia.logoutAction" >
   <result name="success" type="redirect">/pages/uiTags/checkLogin.jsp</result>
</action>

Develop an action class that handles the login request. The Struts 2 framework provides a base ActionSupport class that  implements  commonly used framework  interfaces. In our action class (loginAction.java) we have extended ActionSupport class. 

Here is the code of "loginAction" action class:

package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import java.util.*;

public class loginAction extends ActionSupport {
  private String userId;
  private String password;
  public String execute() throws Exception{

  if ("admin".equals(userId&& "admin".equals(password)) {
  Map session = ActionContext.getContext().getSession();
  session.put("logged-in","true");
  return SUCCESS;
  }
  else{
 return ERROR;
  }
  }

  public String logout() throws Exception {

  Map session = ActionContext.getContext().getSession();
  session.remove("logged-in");
  return SUCCESS;
  }

  public String getPassword() {
  return password;
  }

  public void setPassword(String password) {
  this.password = password;
  }

  public String getUserId() {
  return userId;
  }

  public void setUserId(String userId) {
  this.userId = userId;
  }
}

Download this code.

Again, develop an action class to handle the logout operation. An action class (logoutAction) we have extended ActionSupport class. 

Here is the code of logoutAction action class:

package net.roseindia;
import javax.servlet.http.HttpSession;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import java.util.*;

public class logoutAction extends ActionSupport {
  public String execute() throws Exception 
  Map session = ActionContext.getContext().getSession();
  session.remove("logged-in")
  return SUCCESS;
  }
}

Download this code.

Develop Login Form: The GUI of the application consists of login form (Login.jsp). The "Login.jsp" displays the login page to the user.  

Here is the code of Login.jsp file:

<%taglib prefix="s" uri="/struts-tags" %>
<%page language="java" contentType="text/html"%>

<html>
  <head>
  <title>Insert Data here!</title>
 <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
  type="text/css"/>  
  </head>
  <body>
  <s:form action="/roseindia/login.action" method="POST">
  <s:textfield name="userId" label="Login Id"/><br>
  <s:password name="password" label="Password"/><br>
 <s:submit value="Login" align="center"/>
  </s:form>
  </body>
</html>

The "Success.jsp" page displays the Login Success message (Welcome, you have logged-in.) and session (Session Time: Wed Aug 01 11:26:38 GMT+05:30 2007 and Logout ) when user gets successful authentication. If you click the "Logout" then again login page is displayed on the screen.

Here is the code of Success.jsp file:

<%taglib prefix="s" uri="/struts-tags" %>
<%page language="java"  contentType="text/html" import="java.util.*"%>
<jsp:include page="/struts2tags/pages/uiTags/loginCheck.jsp" />

<html>
  <head>
  <title>Welcome, you have logined!</title>
  </head>
  <body>
  Welcome, you have logined. <br />
  <b>Session Time: </b><%= new Date(session.getLastAccessedTime())%>
  <br /><br />
  <a href="<%= request.getContextPath() %>/roseindia/logout.action">Logout</a>
  <br />
  </body>
</html>

This page logs out the valid user. 

checkLogin.jsp

<%taglib prefix="s" uri="/struts-tags" %>
<%page language="java" contentType="text/html" import="java.util.*"%>
<html>
  <head>
  <title>Check validate!</title>
  </head>
  <body>
  <s:if test="#session.login != 'admin'">
  <jsp:forward page="/pages/uiTags/Login.jsp" />  
  </s:if>
  </body>
</html>

Output:

Run this application by getting the login page:

Enter the wrong user id and password in the login page

You get the following output:

Enter the correct user id and password in the login page:

You get the following output:

After clicking the "Logout". You get the following output:

     

Advertisements

Share on Google+Share on Google+

Login/Logout With Session

Posted on: May 23, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Login/Logout With Session   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:11
Meheid Hasan
June 9, 2013
using session

it's obviously good but may more clear for initial developer
Rajashekhar
September 21, 2011
learning java programing

I heard this is the best site for learning programming
rushan
November 16, 2011
after logout

when i have log out, then if i click the back button of the browser it goes to the previous page with the displaying the content. how can i avoid that?
Anand
December 27, 2011
jar files

Can u please send us jar files for above project
Mahesh
December 28, 2011
struts matirial

roseindia is providing good info.
Mahesh
December 28, 2011
Struts developer

i want struts example application with business logic can any one provide..
aditya
June 9, 2012
logout in struts2

If someone clicks the back button then the browser takes him to my login page even after logout..... there is option in javascript to disable the back button...bt if someone disables javascript then he can easily get into my login page again....so what should i do??? plz do reply....
prakash
August 6, 2012
java

it is not running on my tomcat server.
cherryHYD
November 19, 2012
Help Me...

class A{ protected int i; } class B extends A{ } class C extends B{ } // how to initialize the A class variable in class C without creating the instance of A....

Ads

 

Ads