Struts 2 Email Validator

In the struts, web application, an email validator is used to check whether the entered email by the user is valid or not. Parameter defined with email validator is:

  • fieldName: It is used to specify the name of the field which is to be validated.

Struts Bundled Validators - email Example:

To get input from the user create the index.jsp file:

The index.jsp page takes the input from the user. The user will enter the email and password. Then after clicking on the Register button, it will redirect to the next resource. It contains two textfield and one button on the form with the link to LoginAction class

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>  
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<s:form action="login">
<s:textfield name="email" label="Enter the email"></s:textfield>
<s:textfield name="pwd" label="Enter the password"></s:textfield>
<s:submit value="Register"></s:submit>
</s:form>
</body>
</html>

Create welcome.jsp file to denote success:

The welcome.jsp page is used to display Welcome to the world of the Technology message when execute method return success string.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome page</title>
</head>
<body>
Welcome to the world of the Technology. 
</body>
</html>

Create error.jsp file to denote failure:

The error.jsp page is used to display Sorry wrong Email or Password !!! message on failure. When the execute() method returns an error, this page executes.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Error page</title>
</head>
<body>
Sorry wrong Email or Password !!!
</body>
</html>

Create the action class LoginAction.java:

The Action class LoginAction.java contains two fields email and password with their getter and setters. It contains the execute method. On success, it will go to the welcome.jsp page otherwise on error it will redirect to error.jsp.

import java.util.Date;


import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport
{
private String email;
private String pwd;


public String getEmail() 
{
return email;
}
public void setEmail(String email) 
{
this.email = email;
}
public String getPwd() 
{
return pwd;
}
public void setPwd(String pwd) 
{
this.pwd = pwd;
}


@Override
public String execute() throws Exception 
{
return "success";
}


}

web.xml file is created inside WEB-INF folder in WebContent folder:

The web.xml file in the WEB-INF folder specify how elements are processed. The entry of FilterDispatcher is done in the web.xml file. /* specifies all urls will be parsed. This task is done by struts filter.

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>StrutsBundledValidation</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <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>
</web-app>

Construct struts.xml file:

In the struts.xml file, make the entry of the action class LoginAction and link for it and result pages. The result determines what browser will display after the execution of the action. Results have optional names like success, input and error.

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts  
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
<struts>  
<package name="default" extends="struts-default">  
  
<action name="login" class="LoginAction">  
<result name="success">welcome.jsp</result>  
<result name="error">error.jsp</result>
<result name="input">index.jsp</result>
</action>  
</package>  
</struts> 

Create the validation file:

Create the LoginAction-Validation.xml file to validate Email and password length.

<?xml version="1.0" encoding="UTF-8"?>  
  
  <!DOCTYPE validators PUBLIC   
        "-//OpenSymphony Group//XWork Validator 1.0.2//EN"   
        "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  
  
        <validators>  
          
        <field name="email">  
        <field-validator type="requiredstring">  
        <message>Email can not be blank</message>  
        </field-validator>  
        <field-validator type="email">  
        <message>Email entered is wrong...Please, Enter correct email.</message>  
        </field-validator>  
        </field>        
          
        <field name="pwd">  
        <field-validator type="requiredstring">  
        <message>Password can't be blank</message>  
        </field-validator>  
        <field-validator type="stringlength">  
        <param name="minLength">6</param>  
        <param name="maxLength">10</param>  
        <message>Password must be greater than or equal to 6 and less than or equal to 10</message>  
        </field-validator> 
        </field>
          
                 
        </validators> 

Output:

In order to run the application, right-click on the project -> Click on the option Run As -> then select Run on Server. It will show two labels for entering the name and password and the Register button.

Struts Bundled Validators - email

Enter email as the name only and enter the correct password including letters, digits, and special characters.

Struts Bundled Validators - email

As the Email is not valid, an error will display saying the Email entered is wrong...Please, Enter the correct email.

Struts Bundled Validators - email

Again run the application and this time enter the correct email with @ sign and .com in string.

Struts Bundled Validators - email

This time Email and password are correct and according to the specified validation rule. So, it will redirect to welcome.jsp and display Welcome to the world of technology.

Struts Bundled Validators - email