Simple registration application example using struts tutorial

In this example we will develop the registration application using struts framework. After configuration the struts in web.xml, we are ready to struts based application. Below is the list of files which are developed for registration application.

1) Registration.jsp

2) registrationSuccess.jsp

3) struts-config.xml

4) RegistrationAction.java

5) RegistrationForm.java

6) ApplicationResources.properties

Registration.jsp

Create a registration.jsp page and put it your web application directory, we will use the struts html custom tags library for html and bean tag library for displaying message.

Struts provide a bean tag library, we will use bean tag library to display the message which are defined inside ApplicationResources.properties file.

Struts also provide an html tag library, we will use html tag library to create the html tags like form, text select etc. Struts html tag library is very powerful for generating the html related xml tags, the html custom library also composed with struts framework to invoke the form bean class properties.

We have used the action=”/registration” and styleId=”registrationForm” which need to configure inside struts-config.xml file.

This jsp page also have submit and reset button to submit and reset the form.

The registration.jsp page looks like below code:

 
 
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"  prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"  prefix="bean"%>
 
<html:form action="/registration" styleId="registrationForm">
    <table align="center">
        <tr>
            <td><bean:message key="registration.firstname" /></td>
            <td><html:text property="firstname"></html:text></td>
        </tr>
        <tr>
            <td><bean:message key="registration.lastname" /></td>
            <td><html:text property="lastname"></html:text></td>
        </tr>
        <tr>
            <td><bean:message key="registration.gender" /></td>
            <td>
                <html:select property="gender">
                    <html:option value="">Select</html:option>
                    <html:option value="MALE">Male</html:option>
                    <html:option value="FEMALE">Female</html:option>
                </html:select>
            </td>
        </tr>
        <tr>
            <td><bean:message key="registration.email" /></td>
            <td><html:text property="email"></html:text></td>
        </tr>
        <tr>
            <td><bean:message key="registration.mobile" /></td>
            <td><html:text property="mobile"></html:text></td>
        </tr>
        <tr>
            <td><bean:message key="registration.address" /></td>
            <td><html:text property="address"></html:text></td>
        </tr>
        <tr>
            <td><bean:message key="registration.username" /></td>
            <td><html:text property="username"></html:text></td>
        </tr>
        <tr>
            <td><bean:message key="registration.password" /></td>
            <td><html:password property="password"></html:password></td>
        </tr>
        <tr>
            <td><html:submit property="submit">Submit</html:submit></td>
            <td><html:reset property="reset">Reset</html:reset></td>
        </tr>
    </table>
</html:form>
 
 

ApplicationResources.properties

The ApplicationResources.properties file used to put the key message pair, the message can be get using key by bean tag library as we used in registration.jsp page.

To use the ApplicationResources.properties file you have to configure it inside struts config file as

 
 
<message-resources parameter="ApplicationResources"/>
 
 
 

The ApplicationResources.properties file for registration example looks like below code:

 
 
registration.firstname=First name
registration.lastname=Last name
registration.gender=Gender
registration.mobile=Mobile no
registration.email=E-Mail
registration.address=Address
registration.username=Username
registration.password=Password
 
 
 

RegistrationForm.java

We need to create the form bean class which should be derived from ActionForm class of struts framework. In this example we will create RegistrationForm.java, inside form bean class we need to define the all the properties names in registration.jsp page with their getter and setter method.

The RegisterationForm.java file for registration example looks like below code:

 
 
package com.java.connect.struts.form;
 
import org.apache.struts.action.ActionForm;
 
public class RegistrationForm extends ActionForm {
 
    private static final long serialVersionUID = 1L;
    private String firstname;
    private String lastname;
    private String email;
    private String gender;
    private String mobile;
    private String address;
    private String username;
    private String password;
 
    public String getGender() {
        return gender;
    }
 
    public void setGender(String gender) {
        this.gender = gender;
    }
 
    public String getMobile() {
        return mobile;
    }
 
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
 
    public String getFirstname() {
        return firstname;
    }
 
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
 
    public String getLastname() {
        return lastname;
    }
 
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
    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;
    }
}
 
 
 

RegistrationAction.java

For each action we need to create corresponding Action class, which should be derived from Action class of struts framework. The Action class has one execute () method, where you can write your business logic for your application and using Form bean class. The Form bean class can be access throw type casting of form object which is available as a one of the parameter of execute () method.

The execute() method has return type as ActionForward class, which tell where to forward the request after processing.

In this RegistrationAction.java we are not going to write any logic, we are just forwarding to success.

 
 
package com.java.connect.struts.action;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
 
public class RegistrationAction extends Action {
 
    @Override
    public ActionForward execute(ActionMapping mapping, 
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
 
        return mapping.findForward("success");
    }
}
 
 
 

Struts-config.xml

The struts-config.xml file is used to configure the struts framework for registration application, The file contain the form bean and action configuration.

You can note that we are forwarding success name to registrationSuccess.jsp page.

 
 
<struts-config>
    <form-beans>
        <form-bean name="registrationForm"
            type="com.java.connect.struts.form.RegistrationForm" />
    </form-beans>
 
    <action-mappings>
        <action path="/registration" name="registrationForm"
        type="com.java.connect.struts.action.RegistrationAction" scope="request">
        <forward name="success" path="/registrationSuccess.jsp" />
        </action>
    </action-mappings>
    <message-resources parameter="ApplicationResources" />
</struts-config>
 
 
 

registrationSuccess.jsp

In this page we will display the values of the registration details which has been put while registration.

The registrationSuccess.jsp page looks like below code.

 
 
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"  prefix="bean"%>
 
<table align="center">
    <tr>
        <td colspan="2">Your registration has been done 
            successfully with below informations</td>
    </tr>
    <tr>
        <td><bean:message key="registration.firstname" /></td>
        <td><bean:write name="registrationForm" property="firstname" /></td>
    </tr>
    <tr>
        <td><bean:message key="registration.lastname" /></td>
        <td><bean:write name="registrationForm" property="lastname" /></td>
    </tr>
    <tr>
        <td><bean:message key="registration.gender" /></td>
        <td><bean:write name="registrationForm" property="gender" /></td>
    </tr>
    <tr>
        <td><bean:message key="registration.email" /></td>
        <td><bean:write name="registrationForm" property="email" /></td>
    </tr>
    <tr>
        <td><bean:message key="registration.mobile" /></td>
        <td><bean:write name="registrationForm" property="mobile" /></td>
    </tr>
    <tr>
        <td><bean:message key="registration.address" /></td>
        <td><bean:write name="registrationForm" property="address" /></td>
    </tr>
    <tr>
        <td><bean:message key="registration.username" /></td>
        <td><bean:write name="registrationForm" property="username" /></td>
    </tr>
    <tr>
        <td><bean:message key="registration.password" /></td>
        <td><bean:write name="registrationForm" property="password"/></td>
    </tr>
</table>
 
 
 

Now you have done with development of registration application, now we have to build it and make a war file, and deploy it to tomcat. Restarts the tomcat web server after deploy the registration application.

Now you can access the registration example using the URL http://localhost:8080/java-connect-example/registration.jsp