感觉比较简单,就不多说了,写一个案例就明白了。
用于显示当前在线的人的名字:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="loginServlet" method="post">
用户名: <input type="text" name="username"/><br/>
<input type="submit" value="登录"><br/>
</form>
</body>
</html>
package com.huxin.listener;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineListener implements ServletContextListener,HttpSessionListener,HttpSessionAttributeListener{
private ServletContext application = null;
public void contextDestroyed(ServletContextEvent arg0) {}
//初始化上下文时调用此方法
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("load的时候调用此方法");
//初始化application
this.application = arg0.getServletContext();
this.application.setAttribute("online", new ArrayList<String>());
}
//当session中的属性发生改变的时候调用此方法
public void attributeAdded(HttpSessionBindingEvent arg0) {
System.out.println("属性发生了改变,所以调用了方法");
List<String> online = (List<String>)this.application.getAttribute("online");
if("username".equals(arg0.getName())){
online.add((String) arg0.getValue());
}
this.application.setAttribute("online", online);
}
public void attributeRemoved(HttpSessionBindingEvent arg0) {}
public void attributeReplaced(HttpSessionBindingEvent arg0) {}
public void sessionCreated(HttpSessionEvent arg0) {}
//session被销毁的时候调用此方法
public void sessionDestroyed(HttpSessionEvent arg0) {
//销毁的时候调用此方法
System.out.println("销毁的时候调用此方法");
List<String> online = (List<String>)this.application.getAttribute("online");
String username = (String)arg0.getSession().getAttribute("username");
online.remove(username);
this.application.setAttribute("online", online);
}
}
package com.huxin.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
System.out.println(username);
if(username!=null&&!"".equals(username)){
request.getSession().setAttribute("username", username);
}
List<String> online = (List<String>)this.getServletContext().getAttribute("online");
PrintWriter out = response.getWriter();
out.println("当前的用户是:"+ username+"<br/>");
out.println("<hr>");
out.println("<h3>当前的在线列表为</h3>");
int size = online== null?0:online.size();
for(int i = 0;i<size;i++){
out.println(i+1+"." + online.get(i)+"<br/>");
}
out.println("<hr/><a href=\""+ response.encodeRedirectURL("logoutServlet")+"\">注销</a>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
package com.huxin.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// request.getSession().invalidate();
List<String> online = (List<String>)this.getServletContext().getAttribute("online");
PrintWriter out = response.getWriter();
out.println((String)request.getSession().getAttribute("username"));
out.println("<h3>当前的在线列表为</h3>");
int size = online== null?0:online.size();
for(int i = 0;i<size;i++){
out.println(i+1+"." + online.get(i)+"<br/>");
}
}
}
web.xml中配置listener
<listener>
<listener-class>com.huxin.listener.OnlineListener</listener-class>
</listener>