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.
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.
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";
}
}
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.
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.