Struts

Application-6 : Creating a Email Login Web Application with Struts
Previous Home Next

In this chapter, you create a Email Login web application with Struts from start to end. In this Application, two different struts example like registration and login are concatenation and make a application. For each piece of the application, we show you the code and then explain the code. This application design in NetBeans IDE- 6.8 with Struts 1.3.

Requirement for your struts application-

  • At least one view component is required in every Application.
  • A subclass of ActionForm is used to submit the date as application needed.
  • A subclass of Action class which provide specific processing functionality for the application.
  • A web.xml file is required for calling ActionServlet and struts-config.xml.
  • A struts-config.xml file is required for updating and mapping the update.

Let's Start the application, this application provide a good understanding how struts framework works and also understanding the development of Struts applications.

Now, Following the some steps for develop your New application name as emailLogin. emailLogin application contain following page-

  • Three View file name as index.jsp, welcomeStruts.jsp,registration.jsp, DisplayPage.jsp, login.jsp and success.jsp
  • Two JavaBeans file name as LoginForm.java and RegistrationForm.java used for store data as application need.
  • Two Action file name as LoginAction.java and RegistrationAction.java used for provide specific processing functionality.
  • Two XML file name as web.xml and struts-config.xml file, used for provide control, mapping and validation in application.
  • Step- 1 : Start your IDE ( NetBeans 6.8 with struts 1.3).

    Step- 2 : Open a new project( Crtl+Shift+N), name it , add server( Apache Tomcat 6.0.20 ), then add struts framework 1.3 , and don't forget to add Struts TLDs.

    Note:-When you click Finish button, your NetBeans IDE configure everything in place and project open in your IDE.

    Step- 3: Open index.jsp page, this page automatic configure to call welcomeStruts.jsp page by IDE.

    <%-- Document    : index.jsp
    Description : First Page of lotteryDraw Application--%>
    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>
    <!-- request Global-forward to the welcomeStruts.jsp-->
    <jsp:forward page="Welcome.do"/>
    

    Step- 4: Open welcomeStruts.jsp page from your struts application and code it as describe below.

    <%-- Document: welcomeStruts.jsp
    Description : Second Page of EmailLogin Application--%>
    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>
    <%@ taglib uri="http://struts.apache.org/tags-bean"
    prefix="bean" %>
    <%@ taglib uri="http://struts.apache.org/tags-html"
    prefix="html" %>
    <%@ taglib uri="http://struts.apache.org/tags-logic"
    prefix="logic" %>
    <html:html lang="true">
    <head>
    <meta http-equiv="Content-Type"
    content="text/html; charset=UTF-8">
    <title><bean:message key="welcome.title"/></title>
    <html:base/>
    </head>
    <body style="background-color:threedlightshadow">
    <logic:notPresent name="org.apache.struts.action.MESSAGE"
    scope="application">
    <div  style="color: red">
    ERROR:  Application resources not loaded -- check servlet 
    containerlogs for error messages.
    </div>
    </logic:notPresent>
    <h3><bean:message key="welcome.heading"/></h3>
    <p><bean:message key="welcome.message"/></p>
    <BR>
    <p><b> New User please register yourself by click registration
    page, else click login page</b></p><BR>
    <ul>
    <p> Forward to <a style="cursor:auto ; color:green"
    href="registration.jsp"> Registration </a> Page </p>
    <p> Forward to <a style="cursor: auto; color: green" 
    href="login.jsp"> Login </a> Page </p>
    </ul>
    </body>
    </html:html>
    

    Step- 5: Open registration.jsp page from your struts application and code it as describe below.

    <%--  Document : registration.jsp
    Description : Third Page of EmailLogin Application--%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <%@ taglib uri="http://struts.apache.org/tags-bean"
    prefix="bean" %>
    <%@ taglib uri="http://struts.apache.org/tags-html"
    prefix="html" %>
    <html>
     <head>
      <meta http-equiv="Content-Type" 
      content="text/html; charset=UTF-8">
      <title>Registration page</title>
     </head>
     <body style="background-color:threedlightshadow">
    
      <html:form action="/registration" method="POST">
      <h1><bean:message key="welcome.heading"/></h1>
      <ul>
     <table border="2" cellpadding="5" cellspacing="6" 
     style="background-color:yellow">
     <tbody>
      <tr>
    <td><bean:message key="welcome.userName" />
     <html:errors property="UserName" />
    </td>
    <td><html:text property="userName" size="25" 
    maxlength="10"/> </td>
      </tr>
      <tr>
    <td><bean:message key="welcome.password" />
      <html:errors property="password" />
    </td>
    <td> <html:password property="password" size="25"
    maxlength="10"/> </td>
      </tr>
      <tr>
    <td><bean:message key="welcome.name" />
    <html:errors property="name" />
    </td>
    <td> <html:text property="name" size="25" 
    maxlength="15"/> </td>
      </tr>
      <tr>
    <td> <bean:message key="welcome.age" />
    <html:errors property="age" />
    </td>
    <td> <html:text property="age" size="25" 
    maxlength="2"/> </td>
      </tr>
      <tr>
    <td> <bean:message key="welcome.email" />
    <html:errors property="Email Id" />
    </td>
    <td> <html:text property="email" size="25"
    maxlength="25" /> </td>
      </tr>
      <tr>
    <td><bean:message key="welcome.number" />
    <html:errors property="number" />
    </td>
    <td> <html:text property="number" size="25" 
    maxlength="15" /> </td>
      </tr>
    <tr>
    <td> <bean:message key="welcome.address" />
    <html:errors property="Address" />
    </td>
    <td> <html:textarea property="address" cols="20" 
    rows="4" /> </td>
      </tr>
      <tr>
    <td> <bean:message key="welocme.gender" />
    <html:errors property="gender" />
    </td>
    <td><html:radio property="gender" value="Male" />Male
     &nbsp;&nbsp;&nbsp;<html:radio property="gender" 
     value="feMale" />Female</td>
      </tr>
      <tr>
    <td>&nbsp;&nbsp;<html:submit property="submit" 
    value=" Register "  /></td>
    <td> &nbsp;&nbsp; <html:reset property="reset"
    value=" Reset " /></td>
      </tr>
     </tbody>
    </table>
      </ul>
    
      </html:form>
     </body>
    </html>

    Step- 6: Open DisplayPage.jsp page from your struts application and code it as describe below.

    <%--  Document : DisplayPage
     Description : Forth Page of EmailLogin Application--%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <%@ taglib uri="http://struts.apache.org/tags-bean"
    prefix="bean" %>
    <html>
     <head>
      <%-- Redirect page within the 10 sec, to login.jsp  --%>
      <meta http-equiv="refresh" content="10; URL= login.jsp" >
      <title>Dispaly Page</title>
     </head>
     <body style="background-color:threedlightshadow">
      <h1><bean:message key="welcome.heading"/></h1>
      <h2>Congratulations! You have successfully Register</h2>
      <ul> Your Detail:
    <p> UserName: <bean:write name="RegistrationForm"
    property="userName" /></p>
    <p> Password: <bean:write name="RegistrationForm"
    
    property="password" /></p>
      </ul>
      <ul> <b>
    Please wait for 10 sec, Page will be automatic redirect to
    Login Page otherwise do it manually by click the link given below.
    </b>
    <p>Now, Forward to <a style="cursor:auto; color:green"
    href="login.jsp"> Login </a> Page </p>
      </ul>
     </body>
    </html>
    

    Step- 7: Open login.jsp page from your struts application and code it as describe below.

    <%--  Document: login.jsp
    Description : Fifth Page of EmailLogin Application--%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <%@ taglib uri="http://struts.apache.org/tags-bean"
    prefix="bean" %>
    <%@ taglib uri="http://struts.apache.org/tags-html"
    prefix="html" %>
    <html:html xhtml="true">
    <head>
    <meta http-equiv="Content-Type" 
    content="text/html; charset=UTF-8">
    <title>Login page</title>
    </head>
    <body style="background-color:threedlightshadow">
    <html:form action="/login" method="POST" >
    <h1><bean:message key="welcome.heading"/></h1>
    <BR><ul>
    <html:errors  property="usermatch"/>
    <html:errors  property="passmatch"/>
    <table border="2" cellspacing="6" cellpadding="8" 
    style="background-color: yellow">
    <thead>
    <tr>
    <td>Detail </td>
    <td> Detail From Beans</td>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td> <bean:message key="welcome.name" />
    <html:errors property="UserName" />
    </td>
    <td><html:text property="username" size="25"
    maxlength="10"/></td>
    </tr>
    <tr>
    <td> <bean:message key="welcome.password" />
    <html:errors property="password" />
    </td>
    <td><html:password property="password" size="25" 
    maxlength="10"/></td>
    </tr>
    <tr>
    <td>
    &nbsp;&nbsp;<html:submit property="submit" 
    value=" Check " /></td>
    <td>  
    &nbsp;&nbsp;&nbsp; <html:reset property="reset" 
    value=" Reset " />
    </td>
    </tr>
    </tbody>
    </table>
    </ul>
    </html:form>
    </body>
    </html:html>
    

    Step- 8: Open success.jsp page from your struts application and code it as describe below.

    <%--  Document: success
    Description : Sixth Page of EmailLogin Application--%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <%@ taglib uri="http://struts.apache.org/tags-bean"
    prefix="bean" %>
    <html>
    <head>
    <meta http-equiv="refresh" content="10; URL=index.jsp">
    <title>success</title>
    </head>
       <body style="background-color:threedlightshadow">
    <h1><bean:message key="welcome.heading"/></h1>
    <h2>Congratulations! You have successfully logged in </h2>
    <b> Your Detail here:</b>
    <table style="background-color: aqua" border="1" 
    cellspacing="8" cellpadding="5">
    <tbody> 
    <tr>
    <td> UserName: </td>
    <td><bean:write name="RegistrationForm" 
    property="userName" /></td>
    </tr>
    <tr>
    <td>Passsword: </td>
    <td><bean:write name="RegistrationForm" 
    property="password" /></td>
    </tr>
    <tr>
    <td> Name: </td>
    <td><bean:write name="RegistrationForm" 
    property="name" /></td>
    </tr>
    <tr>
    <td>Age: </td>
    <td><bean:write name="RegistrationForm" 
    property="age" /></td>
    </tr>
    <tr>
    <td> Address: </td>
    <td><bean:write name="RegistrationForm"
    property="address" /></td>
    </tr>
    <tr>
    <td> E-mail Id: </td>
    <td><bean:write name="RegistrationForm" 
    property="email" /></td>
    </tr>
    <tr>
    <td> Phone/Mobile NO: </td>
    <td><bean:write name="RegistrationForm" 
    property="number" /></td>
    </tr>
    <tr>
    <td> Gender:</td>
    <td> <bean:write name="RegistrationForm"
    property="gender" /></td>
    </tr>
    </tbody>
    </table>
    <ul><B> Wait for 10sec page automatic redirect to
    <strong style="color: blue"> Home </strong> page,
    or click link below</B>
    <p>Return to <a style="cursor:auto; color:green" 
    href="index.jsp"> Home </a> page</p>
    </ul>
    </body>
    </html>
    

    Step- 9: Open web.xml file place in WEB-INF folder, this file is used to map the ActionServlet( work as backbone of application, every Action, Validation and ActionForm class control by this class) , struts-config.xml file, welcome file and five different TagLibrary.

    <!-- Save as a web.xml-->
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
    <!-- ActionServlet map here, however you can't find
    this class within the project but it work as a backbone 
    of the project -->
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet
    </servlet-class>
    <!-- Your struts-config.xml file map here-->
    <init-param>
    <param-name>config</param-name>
    <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
    <param-name>debug</param-name>
    <param-value>2</param-value>
    </init-param>
    <init-param>
    <param-name>detail</param-name>
    <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
    </servlet>
    <!-- Action class map with URL -->
    <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <session-config>
    <session-timeout>
    30
    </session-timeout>
    </session-config>
    <!-- Welcome file map here: -->
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <jsp-config>
    <!-- All the Tag Library which used with in the project -->
    <taglib>
    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
    </taglib>
    <taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
    </taglib>
    <taglib>
    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
    </taglib>
    <taglib>
    <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
    </taglib>
    <taglib>
    <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
    </taglib>
    </jsp-config>
    </web-app>
    

    Step- 10: Open struts-config.xml file place in WEB-INF folder, this file is used to map Model class, Exception class, Validation class, Action class( Action class further choose View class) and ApplicationResource class.

    <?xmlversion="1.0"encoding="UTF-8"?>
    <!DOCTYPEstruts-configPUBLIC
    "-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.3//EN"
    "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
    <struts-config>
    <form-beans>
    <form-beanname="LoginForm"type="com.myapp.struts.LoginForm"/>
    <form-beanname="RegistrationForm
    "type="com.myapp.struts.RegistrationForm"/>
    </form-beans>
    <global-exceptions>
    </global-exceptions>
    <global-forwards>
    <forwardname="welcome"path="/Welcome.do"/>
    </global-forwards>
    <action-mappings>
    <actioninput="/login.jsp"name="LoginForm"path="/login"
    scope="session"type="com.myapp.struts.LoginAction">
    <forwardname="SUCCESS"path="/success.jsp"/>
    <forwardname="FAILURE"path="/login.jsp"/>
    </action>
    <actioninput="/registration.jsp"name="RegistrationForm"
    path="/registration"scope="
    sessions"type="com.myapp.struts.RegistrationAction">
    <forwardname="success"path="/DisplayPage.jsp"/>
    <forwardname="failure"path="/registration.jsp"/>
    </action>
    <actionpath="/Welcome"forward="/welcomeStruts.jsp"/>
    </action-mappings>
    <controllerprocessorClass="org.apache.struts.tiles
    .TilesRequestProcessor"/>
    <message-resourcesparameter="com/myapp/struts
    /ApplicationResource"/>
    <!--===========Tilesplugin=========-->
    <plug-inclassName="org.apache.struts.tiles.TilesPlugin">
    <set-propertyproperty="definitions-config"
    value="/WEB-INF/tiles-defs.xml"/>
    <set-propertyproperty="moduleAware"value="true"/>
    </plug-in>
    <!--==========Validatorplugin============-->
    <plug-inclassName="org.apache.struts.validator.ValidatorPlugIn">
    <set-property
    property="pathnames"
    value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
    </plug-in>
    </struts-config
    

    Step- 11: Now, Add bean class into struts-config.xml, since in this Application two different data is store into different Bean, so two different bean class required.

    First bean class name RegistrationForm.java , which contain data of registration.jsp page

    /* Save as a RegistrationForm.java */
    package com.myapp.struts;
    import javax.servlet.http.HttpServletRequest;
    import org.apache.struts.action.*;
    public class RegistrationForm extends 
    org.apache.struts.action.ActionForm {
    private String userName, password, name, 
    	address, email, gender;
    private int number, age;
    public String getUserName() {
    return userName;
    }
    public String getPassword() {
    return password;
    }
    public String getAddress() {
    return address;
    }
    public String getEmail() {
    return email;
    }
    public String getGender() {
    return gender;
    }
    public String getName() {
    return name;
    }
    public int getNumber() {
    return number;
    }
    public void setAddress(String address) {
    this.address = address;
    }
    public void setEmail(String email) {
    this.email = email;
    }
    public void setGender(String gender) {
    this.gender = gender;
    }
    public void setName(String name) {
    this.name = name;
    }
    public void setNumber(int number) {
    this.number = number;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    public void setUserName(String userName) {
    this.userName = userName;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public RegistrationForm() {
    super();
    }
    /* This is the action called from the Struts framework. */
    @Override
    public ActionErrors validate(ActionMapping mapping, 
    	HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();
    if (getName() == null || getName().length() < 1) {
    errors.add("name",
    	new ActionMessage("error.name.required"));
    }
    if (getUserName() == null || getUserName().length() < 1) {
    errors.add("UserName",
    	new ActionMessage("error.UserName.required"));
    }
    if (getPassword() == null || getPassword().length() < 1) {
    errors.add("password", 
    	new ActionMessage("error.Password.required"));
    }
    if (getAddress() == null || getAddress().length() < 1) {
    errors.add("Address",
    	new ActionMessage("error.Address.required"));
    }
    if (getEmail().equals("") || getEmail().indexOf('@') == -1
    	|| getEmail().indexOf('.') == -1
    || getEmail().indexOf("@@") != -1 
    	|| getEmail().indexOf("..") != -1) {
    errors.add("Email Id",
    	new ActionMessage("error.Email.required"));
    }
    if (getAge() < 2) {
    errors.add("age",
    	new ActionMessage("error.age.required"));
    }
    if (getNumber() < 1) {
    errors.add("number",
    	new ActionMessage("error.number.required"));
    }
    if (getGender() == null) {
    errors.add("gender",
    	new ActionMessage("error.gender.required"));
    }
    return errors;
    }
    }
    

    Second bean class name LoginForm.java , which contain data of login.jsp page

    /* Save as a LoginForm.java */
    package com.myapp.struts;
    import javax.servlet.http.HttpServletRequest;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
    public class LoginForm extends org.apache
    	.struts.action.ActionForm {
    private String username,password;
    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;
    }
    public LoginForm() {
    super();
    }
    /* This is the action called from the Struts framework. */
    @Override
    public ActionErrors validate(ActionMapping mapping
    	, HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();
    if (getUsername() == null || getUsername().length() < 1) {
    errors.add("UserName", new ActionMessage
    	("error.UserName.required"));
    }if(getPassword()==null||getPassword().length()<1){
     errors.add("password", new ActionMessage
    	 ("error.Password.required"));
    }
    return errors;
    }
    }
    

    Step-12: Since Application contain two different bean class hence also contain two corresponding Action class

    First Action class name RegistrationAction.java , which is call by the RegistrationForm.java

     /*Save as a RegistrationAction.java */
     package com.myapp.struts;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    public class RegistrationAction extends org
    	.apache.struts.action.Action {
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    /* This is the action called from the Struts framework.
     * @return */
    @Override
    public ActionForward execute
    	(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, 
    	HttpServletResponse response)
    throws Exception {
       return mapping.findForward(SUCCESS);
    }
    }
    

    Second Action class name LoginAction.java , which is call by the LoginForm.java

    /* Save as a LoginAction.java */
    package com.myapp.struts;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
    public class LoginAction extends org.apache
    	.struts.action.Action {
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    /* This is the action called from the Struts framework.
     * @return */
    @Override
    public ActionForward execute(ActionMapping mapping,
    	ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    String key = "";
    ActionErrors errors = new ActionErrors();
    //Http Session create
    HttpSession session = request.getSession(true);
    //Create instance of Bean classes
    com.myapp.struts.RegistrationForm registrationForm = 
    	(RegistrationForm) session.getAttribute
    	("RegistrationForm");
    com.myapp.struts.LoginForm loginForm = (LoginForm) 
    	session.getAttribute("LoginForm");
    //Set Flag
    boolean flag1 = false;
    boolean flag2 = false;
    //Match condition
    if (registrationForm.getUserName()
    	.equals(loginForm.getUsername())) {
    flag1 = true;
    } else {
    errors.add("usermatch", 
    	new ActionMessage("error.usermatch.required"));
    if (errors.size() != 0) {
    saveErrors(request, errors);
    }
    }
    if (registrationForm.getPassword()
    	.equals(loginForm.getPassword())) {
    flag2 = true;
    } else {
    errors.add("passmatch",
    	new ActionMessage("error.passmatch.required"));
    if (errors.size() != 0) {
    saveErrors(request, errors);
    }
    }
    if (flag1 == true && flag2 == true) {
    key = "SUCCESS";
    } else {
    key = "FAILURE";
    }
    return mapping.findForward(key);
    }
    }
    

     

    Run your application:

    Now your application is complete, so now start run your application. Start your server( which you embedded when configure our struts application), then right click your application and click upon Run option or open your welcome( index.jsp) page and press shift+F6.

    Previous Home Next

    Tolal:0 Click:

    Show All Comments

    Did not find what you were looking for leave your name and message. We will revert within 24 hours
    Name:
    eMail:
    Comment / Feedback: