Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Struts 2 Registration Form
3.
1. Input page creation (inx.jsp)
4.
2. Action class creation (RegAct.jsp)
5.
3. A class to store the data (RegDao.jsp)
6.
4. Request to be mapped in (struts.xml), and view components are defined
7.
5. View components are created
8.
Frequently asked questions
8.1.
What is Struts2?
8.2.
Who developed Struts2?
8.3.
What is a web application?
8.4.
What is the function of the Struts.xml File in Struts?
8.5.
What are the steps to create a registration application using struts2?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Struts 2 Registration Form Example

Author Ankit Kumar
0 upvote

Introduction

Many times people need to use the app but not frequently. Sometimes there is a shortage of space in the device, and the user needs to get the apps to use. The solution to these problems is Web Application development. A web application is an application program that exists on remote servers and is used by the user through the internet.

struts cover

Isn't this a great way as it saves a big chunk of memory? Strut 2 is a framework which helps in making the application. It is open-source, Flexible, Industry standard and extendable. It was developed by the Apache software foundation. 

After, knowing the basics now it’s time to learn about the way to create the registration form using struts2.

Struts 2 Registration Form

Suppose data is required by any individual or any company and ask for the registration through forms. As strut 2 is an open source platform it can also make the registration form, and that too with ease.  The important header files are required to be installed and loaded. We will be doing this with UI tags, and data will be saved to the oracle database. Mysql and DB2, and other databases can also be used.

It is now moving to the code part.

We'll create a table for Oracle.

CREATE TABLE  "NinjaUser"   
   (    "NAME" VARCHAR2(4000),   
    "PASSWORD" VARCHAR2(4000),   
    "EMAIL" VARCHAR2(4000),   
    "GENDER" VARCHAR2(4000),   
    "COUNTRY" VARCHAR2(4000)  
   )  
/  


The steps to create a registration form using struts2 are as follows:

  • Input page creation (inx.jsp)
  • Action class creation (RegAct.jsp)
  • A class to store the data (RegDao.jsp)
  • Request to be mapped in (struts.xml), and view components are defined
  • View components are created

1. Input page creation (inx.jsp)

It is a straightforward JSP page that makes use of struts and two UI tags to build a form for user input.

<%@ taglib uri="/struts-tags" prefix="s" %>  
 <s:form action="register">  
<s:textfield name="name" label="UserName"></s:textfield>  
<s:password name="password" label="Password"></s:password>  
<s:textfield name="email" label="Email"></s:textfield>  
<s:radio list="{'male','female'}" name="gender"></s:radio>  
<s:select cssStyle="width:155px;"list="{'india','pakistan','other',}"  
name="country" label="Country"></s:select>  
<s:submit value="register"></s:submit>   
</s:form>  

2. Action class creation (RegAct.jsp)

There are five fields, and one execute method in this Action class. As we know, the struts framework generates an instance of the action class for each request; we send this object to the save function of the RegDao class.

package com.codingninjas;   
public class RegisterAction {  
private String name,password,email,gender,country;  
//setters and getters  
public String execute(){  
    int i=RegisterDao.save(this);  
    if(i>0){  
    return "success";  
    }  
    return "error";  
}  
}  
Registration'

3. A class to store the data (RegDao.jsp)

This class retrieves information from the object of the RegAct class and puts it in the NinjaUser table.

package com.codingninjas;  
import java.sql.*;  
public class RegisterDao {  
  
public static int save(RegisterAction r){  
int status=0;  
try{  
Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con=DriverManager.getConnection(  
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
  
PreparedStatement ps=con.prepareStatement("insert into NinjaUser values(?,?,?,?,?)");  
ps.setString(1,r.getName());  
ps.setString(2,r.getPassword());  
ps.setString(3,r.getEmail());  
ps.setString(4,r.getGender());  
ps.setString(5,r.getCountry());  
          
status=ps.executeUpdate();  
  
}catch(Exception e){e.printStackTrace();}  
    return status;  
}  
}  

4. Request to be mapped in (struts.xml), and view components are defined

The package, action class, and view components are all described in this xml file.

<?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="register" class="com.codingninjas.RegisterAction">  
<result name="success">register-success.jsp</result>  
<result name="error">register-error.jsp</result>  
</action>  
  
</package>  
</struts>  

5. View components are created

We're going to make two view components here: register-success.jsp and register-error.jsp.

Register-success.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>  
Welcome, <s:property value="name"></s:property> 
Register-error.jsp 
<%@ taglib uri="/struts-tags" prefix="s" %>  
Sorry, some error occurred!  
<s:include value="index.jsp"></s:include>  

Frequently asked questions

What is Struts2?

Strut 2 is a framework which helps in making the application. It is open-source, Flexible, Industry standard and extendable. 

Who developed Struts2?

It was developed by the Apache software foundation.

What is a web application?

A web application is an application program that exists on remote servers and is used by the user through the internet. 

What is the function of the Struts.xml File in Struts?

The user can specify every mapping to actions in the struts.xml file so that a certain action is called whenever a specific operation is carried out. It is located in the WEB-INF/classes folder, also known as the configuration file.

What are the steps to create a registration application using struts2?

Firstly, Input page creation (inx.jsp) then in next step action class creation (RegAct.jsp) then in next step create a class to store the data (RegDao.jsp) then the request to be mapped in (struts.xml), and view components are defined finally view components are created

Conclusion

In this blog, we have extensively discussed the process of making registration forms in struts. We saw first that we needed to select a database. Then we went through the code to create different pages and classes. Then we learnt different viewpoints, and in the end, we discussed some of the frequently asked questions related to this. 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Thankyou image
Live masterclass