The original source code has a lot of bugs. The two sessions,one in jsp <% and the other in <s:property are different things.
bug infested source: http://blog.csdn.net/weiyanghuadi/article/details/9081857
I made the following changes:
success2.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.util.*"%>
<%@ page import="com.opensymphony.xwork2.ActionContext;"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome, you have logined!</title>
</head>
<body>
Welcome, you have logined.
<p>
<%
Map session2 = ActionContext.getContext().getSession();
out.println(session2.get("logged-in"));
out.println("<br/>");
out.println("nothing");
%>
</p>
<br />
<b>Session Time: </b><%=new Date(session.getLastAccessedTime())%>
<br />
<br />
<a href="<%=request.getContextPath()%>/vaannila/logoutAction">Logout</a>
<br /> from s:property
<br />
<s:property value="#session2.logged-in"></s:property>
s:property
value="ActionContext.getContext().getSession().get("logged-in")"/s:property
<%
out.println(ActionContext.getContext().getSession()
.get("logged-in"));
%>
<s:property value="#session.getlastAccessedTime()"></s:property>
</body>
</html>
public class logoutAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out
.println("the action name in struts and href in jsp is a little bit of different!");
Map session = ActionContext.getContext().getSession();
System.out.println("inside logout class");
System.out.println(session.get("logged-in"));
session.remove("logged-in");
return SUCCESS;
}
}
public class loginAction1 extends ActionSupport {
private String userId;
private String password;
// public Map session=ActionContext.getContext().getSession();;
@Override
public String execute() throws Exception {
if ("admin".equals(userId) && "admin".equals(password)) {
Map session = ActionContext.getContext().getSession();
session.put("logged-in", "true");
System.out.println("inside execute");
System.out.println(session.get("logged-in"));
return SUCCESS;
} else {
return ERROR;
}
// return SUCCESS;
}
public String logout() throws Exception {
// Map session = ActionContext.getContext().getSession();
// this method here is useless,it should sits in logoutAction.java
/*
* System.out.println("inside logout");
* System.out.println(session.get("logged-in"));
* session.remove("logged-in"); return SUCCESS;
*/
return "nima";
}
bug infested source: http://blog.csdn.net/weiyanghuadi/article/details/9081857
hope bug free:http://pan.baidu.com/share/link?shareid=4002435387&uk=3878681452