Hi all , welcome to our blog , and today we will learn code for Login Logout Example Using Struts 2 :
Step 1 : download struts jar :
visit : https://struts.apache.org/download.cgi , here I am downloading Struts 2.5.32 jars , you can download any other latest one if this is not available in future.
under Essential Dependencies Only:
download zip file : struts-2.5.32-min-lib.zip.
Step 2 : download tomcat :
https://archive.apache.org/dist/tomcat/tomcat-8/v8.0.45/bin/ download apache-tomcat-8.0.45.exe and install it in your local.
Step 3 : create dynamic web project in Eclipse :
File – > New – > Dynamic Web Project
name : LoginLogoutExample

click Next — > Next —> select Generate web.xml as below and click on Finish :

Add tomcat server to eclipse , and then right click on project –> Properties –> Java Build Path –> Libraries –> Add External Jars –> and select all struts jars which we downloaded earlier as in Step 1 –> Apply –> close.
again right click on project –> Properties –> Deployment Assembly –> Add –> Select Java Build Path Entries –> click Next –> Select All Jars and Finish –> Apply –> Apply and close.
Now inside webapp folder we have to create two files : login.jsp and welcome.jsp :
1.login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Login</h2>
<s:form action="login" method="post">
<s:textfield name="username" label="Username" />
<s:password name="password" label="Password" />
<s:submit value="Login" />
</s:form>
<s:fielderror />
</body>
</html>
2.welcome.jsp
<!DOCTYPE html>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h2>Welcome, <s:property value="username" /></h2>
<p><a href="logout">Logout</a></p>
</body>
</html>
modify web.xml file as below :
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>LoginLogoutExample</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
In above web.xml file we configured Front-Controller StrutsPrepareAndExecuteFilter and welcome file as login.jsp
In src/main/java create LoginAction class as below :
LoginAction.java
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {
private String username;
private String password;
public String execute() {
// Check if username and password are hardcoded correctly
if ("admin".equals(username) && "password".equals(password)) {
return SUCCESS;
} else {
addActionError("Invalid username or password");
return ERROR;
}
}
public String logout() {
// Perform logout logic here if needed
return SUCCESS;
}
// Getter and Setter methods for username and 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;
}
}
In WEB-INF folder create a folder with name classes and in this folder we have to add our struts.xml configuration file :
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="test" namespace="/" extends="struts-default">
<action name="login" class="LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/login.jsp</result>
</action>
<action name="logout" class="LoginAction"
method="logout">
<result>/login.jsp</result>
</action>
</package>
</struts>
Our Project structure in Eclipse will look like this :

Remember , you should have added Tomcat to Eclipse as below :

Right click on Project –> Build Path –>Configure Build Path –> check all entries in Libraries properly as below :

Right Click On Project –> Run As –> Run On Server –> Select tomcat 8.0 –> Next –> Finish :
if project run successfully it will open login.jsp page in browser as below :

if entered correct username and password : that is admin and password then it will display welcome page as below :

if entered wrong username or password , it will display login page ..
Happy Learning…