package com.lee.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* Application Lifecycle Listener implementation class GetConnListener
* ServletContextListener :用于监听Web应用启动和关闭
* ServletContextAttributeListener: 用于监听ServletContxt范围(appliaction)内属性的改变
*
*
* ServletRequestListener:用于监听用户的请求
* SerlvetRequestAttributeListener:用于监听ServletReqeust范围(request)内属性的改变
*
* HttpSessionListener:用于监听用户sesion的开始和结束
* HttpSessionAtributeListener:用于监听HttpSession范围(session)内属性的变化.
*/
@WebListener
public class GetConnListener implements ServletContextListener {
/**
* Default constructor.
*/
public GetConnListener() {
// TODO Auto-generated constructor stub
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Web 应用初始化了");
}
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Web 应用关闭了");
}
}
package com.lee.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener;
/**
* Application Lifecycle Listener implementation class MyServletConnextAtrributeListener
*
*/
@WebListener
public class MyServletConnextAtrributeListener implements ServletContextAttributeListener {
/**
* Default constructor.
*/
public MyServletConnextAtrributeListener() {
// TODO Auto-generated constructor stub
}
/**
* @see ServletContextAttributeListener#attributeAdded(ServletContextAttributeEvent)
*/
public void attributeAdded(ServletContextAttributeEvent arg0) {
ServletContext application=arg0.getServletContext();
String name=arg0.getName();
Object value=arg0.getValue();
System.out.println(application+"范围内添加了名为:"+name+",值为"+value+"的属性");
}
/**
* @see ServletContextAttributeListener#attributeReplaced(ServletContextAttributeEvent)
*/
public void attributeReplaced(ServletContextAttributeEvent arg0) {
ServletContext application=arg0.getServletContext();
String name=arg0.getName();
Object value=arg0.getValue();
System.out.println(application+"范围内名为:"+name+",值被替换为"+value+"的属性");
}
/**
* @see ServletContextAttributeListener#attributeRemoved(ServletContextAttributeEvent)
*/
public void attributeRemoved(ServletContextAttributeEvent arg0) {
ServletContext application=arg0.getServletContext();
String name=arg0.getName();
Object value=arg0.getValue();
System.out.println(application+"范围内名为:"+name+",值为"+value+"的被删除了!");
}
}
package com.lee.listener;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* Application Lifecycle Listener implementation class OnLineListener
*
*/
@WebListener
public class OnLineListener implements HttpSessionListener {
/**
* Default constructor.
*/
public OnLineListener() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpSessionListener#sessionCreated(HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
HttpSession session=arg0.getSession();
//获取sessionid
String sessionid=session.getId();
ServletContext application=session.getServletContext();
if(session.isNew()){//如果是一次新的回话
String user=(String)session.getAttribute("user");
user=(user==null)? "游客":user;
Map<String,String> online=(Map<String,String>)application.getAttribute("online");
if(online==null){
online=new HashMap<String,String>();
}
online.put(sessionid, user);
application.setAttribute("online", online);
}
}
/**
* @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
HttpSession session=arg0.getSession();
ServletContext application=session.getServletContext();
String sessionid=session.getId();
Map<String,String> online=(Map<String,String>)application.getAttribute("online");
if(online!=null)
online.remove(sessionid);
application.setAttribute("online",online);
}
}
package com.lee.listener;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* Application Lifecycle Listener implementation class RequestListenter
*
*/
@WebListener
public class RequestListenter implements ServletRequestListener, ServletRequestAttributeListener {
/**
* Default constructor.
*/
public RequestListenter() {
// TODO Auto-generated constructor stub
}
/**
* @see ServletRequestListener#requestDestroyed(ServletRequestEvent)
*/
public void requestDestroyed(ServletRequestEvent arg0) {
// TODO Auto-generated method stub
System.out.println("请求被初始化");
}
/**
* @see ServletRequestAttributeListener#attributeAdded(ServletRequestAttributeEvent)
*/
public void attributeAdded(ServletRequestAttributeEvent arg0) {
// TODO Auto-generated method stub
ServletRequest request=arg0.getServletRequest();
String name=arg0.getName();
Object value=arg0.getValue();
System.out.println(request+"范围内添加了名为"+name+",值为"+value+"的属性");
}
/**
* @see ServletRequestAttributeListener#attributeRemoved(ServletRequestAttributeEvent)
*/
public void attributeRemoved(ServletRequestAttributeEvent arg0) {
ServletRequest request=arg0.getServletRequest();
String name=arg0.getName();
Object value=arg0.getValue();
System.out.println(request+"范围内删除了名为"+name+",值为"+value+"的属性");
}
/**
* @see ServletRequestAttributeListener#attributeReplaced(ServletRequestAttributeEvent)
*/
public void attributeReplaced(ServletRequestAttributeEvent arg0) {
ServletRequest request=arg0.getServletRequest();
String name=arg0.getName();
Object value=arg0.getValue();
System.out.println(request+"范围内替换了名为"+name+",值为"+value+"的属性");
}
/**
* @see ServletRequestListener#requestInitialized(ServletRequestEvent)
*/
public void requestInitialized(ServletRequestEvent arg0) {
HttpServletRequest request=(HttpServletRequest)arg0.getServletRequest();
HttpSession session =request.getSession();
String sessionid=session.getId();
String ip=request.getRemoteAddr();
String page=request.getRequestURI();
String user=(String)session.getAttribute("user");
user=(user==null)?"游客":user;
try {
//将用户登录存入 数据库 通过sessionid 查询.
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("---发出---"+request.getRequestURI()+
"Addr:"+request.getRemoteAddr()+
"Host:"+request.getRemoteHost()+
"Port:"+request.getRemotePort()+
"User:"+request.getRemoteUser()+
"SessionId:"+request.getRequestedSessionId());
System.out.println("请求被初始化");
}
}