Before 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 until 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 2
In this example, we are need following pages
- index.jsp for providing links to the login, logout and profile.
- struts.xml for defining the result and action.
- Login.java for defining login and logout logic.
- LoginDao.java for matching username and password in the database.
- Profile.java for checking if the user is logged in or not.
- View components for the displaying results.
1) Create index.jsp for input
This 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.xml
This 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 "-
- Struts Configuration 2.1
- "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 logout
This 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 user
This 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 class
This 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 components
There are many view components:
- login.jsp
- loginsuccess.jsp
- loginerror.jsp
- logoutsuccess.jsp
- profilesuccess.jsp
- profileerror.jsp
view components for login
login.jsp
This 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>
loginsuccess.jsp
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"/>
loginerror.jsp
This page displays the error message.
- Sorry username or password error!
- <jsp:include page="login.jsp"></jsp:include>
view components for logout
logoutsuccess.jsp
This page simply displays the successfully logged out message.
- <jsp:include page="index.jsp"></jsp:include>
- <hr/>
- You are successfully logged out!
view components for profile
profilesuccess.jsp
This page prints the welcome to profile message.
- <jsp:include page="index.jsp"></jsp:include>
- <hr/>
- <br/>Welcome to profile
profileerror.jsp
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>