tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Struts2 > Basic > Simple Login Demo

Simple Login Demo 

This example shows the basic login application demo using Struts2. The necessary libraries are shown below,

struts2_libs
The first important setting is FilterDispatcher configuration in web.xml
struts2_dispatcher

Next we need to configure struts.xml with action url path and associated java action class. By default struts2 executes public String execute() method as action method and the return string is used to identify the result. Here we have specified two results namely 'success' which forwards to '/success.jsp' and 'failure' which forwards to '/failure.jsp'. The struts.xml file has to be in class path.

File Name  :  
/Login/src/struts.xml 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com

If the user entered user name and password is same then user login in treated as success otherwise failure. The action class is shown below,

File Name  :  
/Login/src/mypack/Login.java 
   
package mypack;

public class Login {
    String user;
    String pwd;

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }
    public String execute(){
        if(user.equals(pwd)){
            return "success";
        }
        return "failure";
    }
}
   

We can observe that user and pwd field are not initialized with values from request using request.getParameter("fieldName") in action class and the action is just a POJO. Strut2 simplifies this task with 'Parameter Interceptors'. It automatically binds the user entered values with action properties. We can observe that the name of textfield and password field matches with properties in 'Login' class.

Struts2 provides a tab library which simplifies the page rendering. On clicking the submit button it gets submitted to http://localhost:8080/Login/demo/login.action assuming you are using Tomact with default port configuration. The login page is shown below,

File Name  :  
/Login/WebContent/index.jsp 

Success page is shown below,

File Name  :  
/Login/WebContent/success.jsp 

Failure page is shown below,

File Name  :  
/Login/WebContent/failure.jsp 

Login Demo Project Download (4 KB)

Login Demo Libs Download (3 MB)


                    Print

   



Books Recommendation

 
 
bl  br