Pages Navigation Menu

Coding is much easier than you think

Login & Logout Example in Struts 2

 
In this tutorial we will see how to create a simple Struts 2 Login Application. The following files are needed to create a Login Application.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 
Final project structure
 
Login Action Example in struts 2
 

  1. Create a New Dynamic Web Project in eclipse and give a name to your project
  2.  
    Action class
     
    file : LoginAction.java

    package com.action;
    
    public class LoginAction {
    
    	private String userName;
    	private String password;
    
    	public String execute() {
    		if (userName.isEmpty() || password.isEmpty())
    			return "error";
    		else
    			return "success";
    	}
    
    	public String getUserName() {
    		return userName;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    }
    

     
    JSP
     
    File : login.jsp use the Struts 2 tags to display username and password input fields and submit button.
     

    <%@taglib uri="/struts-tags" prefix="s"%>
    <html>
    <head>
    <title>Login</title>
    </head>
    <body>
    	<s:form action="login">
    		<s:textfield name="userName" label="User Name" />
    		  <s:password name="password" label="Password" />
    		<s:submit value="Submit" />
    	</s:form>
    </body>
    </html>
    

     

    file : success.jsp :€“ A JSP view page to display a welcome message to user.
     

    <%@taglib uri="/struts-tags" prefix="s"%>
    <html>
    <head>
    <title>Welcome</title>
    <s:head />
    </head>
    <body>
    <h4>
    	Welcome <s:property value="userName" /> 
    	, you are logged in successfully
    </h4>
    </body>
    </html>
    

     
    file :error.jsp :€“ A JSP view page to display error message when a wrong user id and password is entered
     

    <html>
    <head>
    <title>Un Authorised</title>
    </head>
    <body>
    	<h1>Login failed</h1>
    </body>
    </html>
    

     
    Struts configuration file
     
    The configuration in struts.xml should be as shown below

    <struts>
        <package name="default" extends="struts-default">
    	<action name="login" class="com.action.LoginAction">
    		<result name="success">success.jsp</result>
    		<result name="error">error.jsp</result>
    	</action>
       </package>
    </struts>
    

     
    web.xml
     
    Make sure you have done the mapping properly in web.xml file as shown below

    <web-app>
    <filter>
     <filter-name>struts2</filter-name>
      <filter-class>
    	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
    </filter>
    <filter-mapping>
          <filter-name>struts2</filter-name>
          <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
    	<welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
    </web-app>
    

     
    Demo
     
    Now on running this application in TomCat server, the application will deliver the JSP as below

    http://localhost:8089/Struts2Login/

     
    Login IN struts 2
     
    ** Update – The below screen shot is the demo of Tab Style Login and Signup feature using jQuery in Java web application, read the article here to learn about the same
     
    Tab Style Login Signup using jQueryin Java
     

    About Mohaideen Jamil


    Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.