Example of login form validation in struts2.2.1framework.
Posted on: December 31, 2010 at 12:00 AM
In this tutorial, you will see login form validation in struts2.2.1

Example of login form validation in struts2.2.1 framework.

In this example, we will introduce you to about the login form validation in struts2.2.1 framework. Our login form validation
example does not validate the user against the database. Instead login name and password are validated against the hard coded
values (Username: Bharat and Password: roseindia) in the action class.

Directory structure of Login form validation example.

Description of login validation example:

Step 1:- The index.html is our first page. There is a hyperlink on this index page. It is a simple html page.ADS_TO_REPLACE_1

Step 2 :-  Login.jsp When we will click on hyperlink (available on index.html). Then it will we display on browser to take the user  input.

Step 3 :-  User validation is done in action class and if user enters Bharat/roseindia in the user username/password fields, then success pages is displayed. Otherwise the error message is displayed on the browser.

 1- index.html  ADS_TO_REPLACE_2

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body><center>
<h1>.............. Home Page..........</h1>
<h2>For login please visit given link</h2>
<a href="login.action"> User login Page</a></center>
</body>
</html>

2- Login.jsp
It display a user interface for login user. Which takes the user input.

<%page language="java" contentType="text/html; charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%>
 <%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Insert message</title>
</head>
<body><center>
<h2>Please enter User name & Password</h2>
<s:form action="index" method="post">
<s:textfield  name="username"  key="username"></s:textfield>
<s:password  name="password" key="password"></s:password>
<s:submit></s:submit>
</s:form></center>
</body>
</html>

LoginValidation.java 
It validate the user. If user is valid then the execute method returns success otherwise returns error. 

package roseindia;
import com.opensymphony.xwork2.ActionSupport;

public class LoginValidation extends ActionSupport{
 private String username;
 private String password;
  public String execute() throws Exception {
if(this.getUsername().equals("Bharat")&& this.getPassword().equals("roseindia"))
{
  return SUCCESS;
  }
else
{
  return ERROR;
  }
  }
  public String getUsername() {
    return username;
  }
  public void setUsername(String username) {
    this.username = username;
  }
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }
}

struts.xml

It is a XML file. Here, we will create action mapping of action class.

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
    <package name="sample" extends="struts-default">
          <action name="index" class="roseindia.LoginValidation">
            <result name="success">/jsp/welcome.jsp</result>   
            <result name="error">/jsp/error.jsp</result>        
      </action>
      <action name="login">
                  <result name="success">./jsp/login.jsp</result>         
      </action>   
   </package>
  </struts>

web.xml
Web.xml is used to configuration the servlet container properties of login validation example. The filter and the filter-mapping elements are used for including the Struts 2.2.1 StrutsPrepareAndExecuteFilter. It means all the incoming request that targets to the Struts 2.2.1 action will be handled by StrutsPrepareAndExecuteFilter class.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id="WebApp_ID" version="2.5">
  <display-name>SendMessage</display-name>  
    <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>index.html</welcome-file> 
  </welcome-file-list>
</web-app>

welcome.jsp
If execute method of LoginValidation class returns success then it will display, otherwise error.jsp page will be display. It also also displayed username and password of login user.

<%page language="java" contentType="text/html; charset=ISO-8859-1"   pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body><center>

<H1>....... Login user Information ........</H1><br/>
Name : <s:property value="username"/><br></br>
Password : <s:property value="password"/>
<h2>----------Login success------------</h2>
</center>
</body>
</html>

index.htmlADS_TO_REPLACE_3

Login.jsp

ADS_TO_REPLACE_4

error.jsp

ReloginADS_TO_REPLACE_5

Welcome.jsp

ADS_TO_REPLACE_6

Download Select Source Code


Related Tags for Example of login form validation in struts2.2.1framework.:

Advertisements

Ads

Ads

 
Advertisement null

Ads