Struts 2 Login and Logout ExampleBefore creating the login and logout application using struts 2, you must clear the concepts of aware interfaces in struts 2. In this example, we have used the SessionAware interface to put the information in the session scope and ServletActionContext class to get the information from the session scope. This example contains three links login, logout and profile. The end user cannot click on the profile page untill he/she is logged in. After getting logged in, he/she may go the profile page. If the end user clicks on the logout page, he will not be able to access the profile page. Here, we assume that you have a table in oracle database named user3333 that contains records. The table query is:
CREATE TABLE "USER3333"
( "ID" NUMBER,
"NAME" VARCHAR2(4000),
"PASSWORD" VARCHAR2(4000),
"EMAIL" VARCHAR2(4000),
CONSTRAINT "USER3333_PK" PRIMARY KEY ("ID") ENABLE
)
/
Example of creating login and logout application using struts 2In this example, we are need following pages
1) Create index.jsp for inputThis jsp page creates three links for login, logout and profile. index.jsp<hr/> <a href="login">login</a>| <a href="logout">logout</a>| <a href="profile">profile</a> 2) Define action and result in struts.xmlThis xml file defines one package and 4 actions. Each action defines at least one result page. For the loginprocess and logout actions, we are using the same action class but there invocation methods are different. struts.xml<?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="abc" extends="struts-default"> <action name="login"> <result >login.jsp</result> </action> <action name="loginprocess" class="com.javatpoint.Login"> <result name="success" >loginsuccess.jsp</result> <result name="error" >loginerror.jsp</result> </action> <action name="logout" class="com.javatpoint.Login" method="logout"> <result name="success" >logoutsuccess.jsp</result> </action> <action name="profile" class="com.javatpoint.Profile"> <result name="success" >profilesuccess.jsp</result> <result name="error" >profileerror.jsp</result> </action> </package> </struts> 3) Create the action class for login and logoutThis action class implements the SessionAware interface and overrides the setSession method to store the information in the session scope. For logout, we are simply calling the invalidate() method of SessionMap. Login.java
package com.javatpoint;
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";
}
}
4) Create the Dao class to authenticate userThis class simply validates the user from the table stored in the oracle database. LoginDao.java
package com.javatpoint;
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 user3333 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;
}
}
5) Create the Profile classThis class gets the information from the session scope, if any information is found in the session scope with login name, it returns success otherwise false. Profile.java
package com.javatpoint;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
public class Profile {
public String execute(){
HttpServletRequest request=ServletActionContext.getRequest();
HttpSession session=request.getSession();
String s=(String)session.getAttribute("login");
if(s!=null && !s.equals("")){
return "success";
}
else{
return "error";
}
}
}
6) Create view componentsThere are many view components:
view components for loginlogin.jspThis page creates the login form. <jsp:include page="index.jsp"></jsp:include> <hr/> <%@ taglib uri="/struts-tags" prefix="s" %> <s:form action="loginprocess"> <s:textfield name="username" label="Name"></s:textfield> <s:password name="userpass" label="Password"></s:password> <s:submit value="login"></s:submit> </s:form> This page prints the welcome message with the username. <jsp:include page="index.jsp"></jsp:include> <hr/> <%@ taglib uri="/struts-tags" prefix="s" %> <br/>Welcome, <s:property value="username"/> This page displays the error message. Sorry username or password error! <jsp:include page="login.jsp"></jsp:include> view components for logoutlogoutsuccess.jspThis page simply displays the successfully logged out message. <jsp:include page="index.jsp"></jsp:include> <hr/> You are successfully logged out! view components for profileprofilesuccess.jspThis page prints the welcome to profile message. <jsp:include page="index.jsp"></jsp:include> <hr/> <br/>Welcome to profile This page prints the message to login first and includes the login.jsp page. Please login first to see profile <jsp:include page="login.jsp"></jsp:include> |
| Tweet |
|











