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>
4 Comments
Unknown User (jbigfoot)
It is a dirty WW example because it not use ww tags and getting access to session not using SessionAware
Unknown User (dwangel)
I'm beginner of Webwork.
But I used ww tags a little in jsp and I didn't find SessionAware in the cookbook of accessing session.
Kurt Krems
Minor bug on this:
session.put("logined","true");
session.put("login","true");
Not a bad example.
Ajit Kumar
There is yet another bug.... I'm amazed no one noticed it.... its a fundamental error
The tag <ww:if test="#session.login != 'true'"> will not work and it will always return false.
Correct form of the statement would be <ww:if test="#session.login0 != 'true'">
Reason :
The reason is that #parameters would return a Map, the framework conveniently makes request parameters into a Map to make unit testing action easier and the framework also uses request.getParameterMap();which returns a Map where the key is the parameter (String) while the value is an array of the parameter value (Array of String), hence the need to use [ 0 ].