Apache Struts 2 Documentation > Home > Tutorials > Examples > SimpleLogin with Session
#editReport()

I wrote this simple application to demostrate login/logout.

/Login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head><body>
<form action="login.action" method="post">
User id<input type="text" name="userId" /> <br/>
Password <input type="password" name="passwd" /> <br />
<input type="submit" value="Login"/>
</form>
</body>

</html>

 

 

/pages/welcome.jsp

 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="ww" uri="/webwork" %>
<jsp:include page="WEB-INF/inc/loginCheck.jsp" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
</head>

<body>Welcome, you have logined. <br />
The attribute of 'context' in session is
<ww:property value="#session.context" />
<br /><br /><br />
<a xhref="<%= request.getContextPath() %>/logout.action">Logout</a>
<br />
<a xhref="<%= request.getContextPath() %>/logout2.action">Logout2</a>
</body>
</html>

 

 

/WEB-INF/inc/loginCheck.jsp

 <%@ taglib="/webwork" prefix="ww" %>
<ww:if test="#session.login != 'true'">
<jsp:forward page="<%= request.getContextPath() %>/login.jsp" />
</ww:if>

 

 

 

simple.LoginAction.java

package simple;
import java.util.Date;import java.util.Map;

import javax.servlet.http.HttpSession;

import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionSupport;

public class LoginAction extends ActionSupport {

    private String userId;
    private String passwd;

    public String execute() throws Exception {
        if ("admin".equals(userId) && "password".equals(passwd)) {
//            HttpSession session = ServletActionContext.getRequest().getSession();
//            session.setAttribute("logined","true");
//            session.setAttribute("context", new Date());
// Better is using ActionContext 
  Map session = ActionContext.getContext().getSession();
session.put("logined","true");
            session.put("context", new Date());
            return SUCCESS;
        }
        return ERROR;
    }

    public String logout() throws Exception {
//        HttpSession session = ServletActionContext.getRequest().getSession();
//        session.removeAttribute("logined");
//        session.removeAttribute("context"); 
 Map session = ActionContext.getContext().getSession();
 session.remove("logined");
        session.remove("context");
        return SUCCESS;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }
}

 

 

 simple.LogoutAction.java

package simple;
import java.util.Map;
import javax.servlet.http.HttpSession;

import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionSupport;

public class LogoutAction extends ActionSupport {

    public String execute() throws Exception { 
     Map session = ActionContext.getContext().getSession();
session.remove("logined"); 
session.remove("context");
        return SUCCESS;
    }

}

 

 

 /WEB-INF/classes/xwork.xml

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">

<xwork>
    <include file="webwork-default.xml"/>

    <package name="default" extends="webwork-default">
        <!-- Add your actions here -->
        <action name="login" class="simple.LoginAction" >
            <result name="success" type="dispatcher">/pages/welcome.jsp</result>
            <result name="error" type="redirect">/login.jsp</result>
        </action>

        <action name="logout2" class="simple.LoginAction" method="logout" >
            <result name="success" type="redirect">/login.jsp</result>
        </action>

        <action name="logout" class="simple.LogoutAction" >
            <result name="success" type="redirect">/login.jsp</result>
        </action>
    </package>
</xwork>