Struts is an open-source Java framework that Apache designs. Apache provides users a free source to download Struts. Struts follow MVC architecture; Struts also consist of models: Model0, Model1, and Model2. It provides execution of MVC architecture
Most of the applications ask the user to log in. In this way, they provide a personalised user experience. An account is a section in which user update their personal details and choices. So, through the data provided by the user's account, the company provides the user with a personalised experience. This blog will provide the knowledge to create a login and logout.
Login and Logout Example
While creating the login and logout application, we are going to use SessionAware interface. We will give session scope information through SessionAware, and ServletActionContext class will be used to acquire the information from the session scope.
This example has three links: login, logout, and profile. The end user cannot access the profile page unless he or she is logged in. Users may access the profile page after logging in. The end user will be unable to view the profile page if he clicks on the logout page.
Firstly we will consider a table ninja100 in the oracle database. The query of the table is as follows:
In this action class, we implement the SessionAware interface and override the setSession function to save data to the session scope.
We use the invalidate() function of SessionMap to logout.
Login.java
package com.codingninjas;
import java.util.Map;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
public class Login implements SessionAware{
private String username,userpass;
SessionMap<String,String> sessionmap;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
public String execute(){
if(LoginDao.validate(username, userpass)){
return "success";
}
else{
return "error";
}
}
public void setSession(Map map) {
sessionmap=(SessionMap)map;
sessionmap.put("login","true");
}
public String logout(){
sessionmap.invalidate();
return "success";
}
}
Logindao.java For Authentication Of The User
Here the user’s data will be cross-checked from the oracle database.
LoginDao.java
package com.codingninjas;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class LoginDao {
public static boolean validate(String username,String userpass){
boolean status=false;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement ps=con.prepareStatement(
"select * from ninja100 where name=? and password=?");
ps.setString(1,username);
ps.setString(2,userpass);
ResultSet rs=ps.executeQuery();
status=rs.next();
}catch(Exception e){e.printStackTrace();}
return status;
}
}
Profile.java To Check User Status
This class retrieves information from the session scope; if any information with the login name is discovered in the session scope, it returns success; otherwise, it returns false.
Which interface is used while creating a login and logout application?
SessionAware interface is used while creating a login and logout application.
Who developed Struts2?
The Apache software foundation developed it.
What is the need for an account on the application?
In this way, they provide a personalised user experience. An account is there in which user update their details and choices what kind of content they expect. So, through the data provided by the user's account, the company provides the user with a personalised experience.
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 are the steps to create a login and logout application?
Firstly we create inx.jsp for links to logout and login page. Then we create struts.xml to define the result and action. After that we create Login.java to define login and logout logic. Then we create LoginDao.java for cross-checking username and password in the database. Then we create Profile.java to check whether the user is logged out or not. At the end View components for displaying results.
Conclusion
In this blog, we have extensively discussed the process of making login and logout applications in struts. We saw first that we needed to select a database. Then we went through the code and how to create different classes and methods. Then we learnt different viewpoints, and in the end, we discussed some of the frequently asked questions related to this.