Simple Login Validation Using Struts – Struts Example
A simple login validation using Struts2. This code is tested using NetBeans 8.0.2

  1. File–>New Project–>Java –>Web Application
    create1
  2. Enter a web application name and click Next
    create2
  3. Next –>and again Next
    create3
  4. Select Struts2 –>Finish
    create4

JSP Files:
Create 3 JSP files,
1. login.jsp
2. success.jsp
3. error.jsp

Right Click source Packages and create 2 Java packages with name: Model and Dao
Inside Model package create a Java class file
login.java

Inside Dao package create a Java class file
dao.java

Directory Structure:
structure

login.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Page</title>
    </head>
    <body>
        <s:form action="check">  
        <s:textfield name="name" label="Login Name"/>
        <s:textfield type="password" name="password" label="Password"/>
        <s:submit value="Login"/>
        </s:form>

    </body>
</html>

success.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Successfully Logged in!!!</h1>
    </body>
</html>

error.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Error: Username or Password is incorrect!</h1>
    </body>
</html>

login.java

public class login {
    
    String name;
    String password;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

dao.java

import com.opensymphony.xwork2.ActionSupport;
public class dao extends ActionSupport {
    String name,password;
    
    public String execute()
    {
        if((name.equals("Alpha"))&&(password.equals("alpha")))
        {
            return "success";
        }
        else
        {
            return "error";
        }
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

struts.xml

<struts>
    <include file="example.xml"/>
    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">
        <action name="check" class="Dao.dao" method="execute">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

web.xml

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Output:
output
output1

output3
output4

 

 

 

 

 

 

 

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *