Session会话监听程序

Jsp 页面

sessionlistenertest_form.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<%
   request.setCharacterEncoding("utf-8");
   String name=request.getParameter("name");
   String identity=request.getParameter("identity");
   if(name!=null&&identity!=null)
   {
       session.setAttribute("name",name);
       session.setAttribute("identity",identity);
       response.sendRedirect("display.jsp");
   }
 %>
<body>
<form action="sessionlistenertest_form.jsp" method="post">
<table align="center" border="1">
<tr><td>姓名:</td><td><input type="text" name="name"/></td></tr>
<tr><td>身份:</td><td><input type="text" name="identity"/></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="提交"/></td></tr>
</table>
</form>
</body>
</html>
display.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
 </head>
<body>
<%
  if(session.isNew())
  {
     response.sendRedirect("sessionlistenertest_form.jsp");
  }
   out.println("name: "+session.getAttribute("name")+"<br/>");
   out.println("identity: "+session.getAttribute("identity")+"<br/>"); 
   out.println("session id: "+session.getId()+"<br/>");
   out.println("create time: "+session.getCreationTime());
  
 %>
<table cellspacing="2" cellpadding="10"><tr><td><a href="session.jsp">继续会话监听</a></td>
   <td><a href="logout.jsp">注销</a> </td></tr></table>
</body>
</html>
session.jsp


<%@ page language="java" contentType="text/html; charset=utf-8" import="java.util.*,listener.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
 </head>
<body>
<table border="1">
<tr align="center"><td>会话编号</td><td>用户名</td><td>身份</td><td>创建时间</td><td>操作</td></tr>

<%
	Iterator it=listener.SessionListener.getSet();
	while(it.hasNext())
	{
	   HttpSession session1=(HttpSession)it.next();
	   %>
	   <tr><td><%=session1.getId() %></td>
	   <td><%=session1.getAttribute("name") %></td>
	   <td><%=session1.getAttribute("identity") %></td>  
	   <td><%=session1.getCreationTime() %></td> 
	   <td><a href="deleteSession.jsp?sessionId=<%=session1.getId() %>">删除</a></td> 	    
	   </tr>
	   <%
	   
	   System.out.println("sessionId:"+session.getId());
	}
%></table>
</body>
  </html>
deleteSession.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" import="java.util.*,listener.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
 </head>
<body>

<%
	String sessionId=request.getParameter("sessionId");
	//System.out.println("deleteSession.jsp:"+sessionId);
	HttpSession session1=SessionListener.getSessionById(sessionId);
	if(session1!=null)
	{
	   session1.invalidate();
	}
	response.sendRedirect("session.jsp");
%>
</body>
  </html>

logout.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" import="java.util.*,listener.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
 </head>
<body>

<%
   if(!session.isNew())
   {
	 session.invalidate();
	}
	response.sendRedirect("sessionlistenertest_form.jsp");
%>
</body>
  </html>


你可能感兴趣的:(Session会话监听程序)