package com.yd.listener;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws
ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out =
response.getWriter();
HttpSession session =
request.getSession();
session.setMaxInactiveInterval(5); //5秒,失效时间
session.setAttribute("MyShopCar",
new MyShopCar());
response.sendRedirect("/index.jsp");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws
ServletException, IOException {
this.doGet(request,
response);
}
}
package com.yd.listener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public class ServletRequeseList implements
ServletRequestListener { //监听计数属性
public void
requestDestroyed(ServletRequestEvent arg0) {
System.out.println("等待退出");
}
public void
requestInitialized(ServletRequestEvent arg0) {
Integer n = null;
n = (Integer)
arg0.getServletContext().getAttribute("REQUESE_COUNT");
if(n != null){
arg0.getServletContext().setAttribute("REQUESE_COUNT",new
Integer(n.intValue()+1));
}else{
n = new
Integer(1);
arg0.getServletContext().setAttribute("REQUESE_COUNT",n);
}
}
}
package com.yd.listener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class MyShopCar implements HttpSessionBindingListener
{ //唯一不需要web.xml中设定Listener,绑定
public void valueBound(HttpSessionBindingEvent
arg0) {
System.out.println(arg0.getName()+"被加入Session");
Integer in = null;
in = (Integer)
arg0.getSession().getServletContext().getAttribute("BINDING_COUNT");
if(in != null){
arg0.getSession().getServletContext().setAttribute("BINDING_COUNT",
new Integer(in.intValue()+1));
}else{
in = new
Integer(1);
arg0.getSession().getServletContext().setAttribute("BINDING_COUNT",
new Integer(in.intValue()));
}
}
public void
valueUnbound(HttpSessionBindingEvent arg0) {
System.out.println(arg0.getName()+"已经做好资源保存工作");
Integer in = null;
in = (Integer)
arg0.getSession().getServletContext().getAttribute("BINDING_COUNT");
if(in != null){
arg0.getSession().getServletContext().setAttribute("BINDING_COUNT",
new Integer(in.intValue()-1));
}else{
in = new
Integer(1);
arg0.getSession().getServletContext().setAttribute("BINDING_COUNT",
new Integer(in.intValue()));
}
}
}
package com.yd.listener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class MyServletHttpSessionAttributeListener
implements
HttpSessionAttributeListener
{ //Session增加,删除,修改时此接口进行监听
public void
attributeAdded(HttpSessionBindingEvent arg0) {
System.out.println("Session中增加了一个属性:"+arg0.getName());
}
public void
attributeRemoved(HttpSessionBindingEvent arg0) {
System.out.println("Session被删除"+arg0.getName());
}
public void
attributeReplaced(HttpSessionBindingEvent arg0) {
System.out.println("Session被替换"+arg0.getName());
}
}
package com.yd.listener;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class MyServletContextAttributeListener implements
ServletContextAttributeListener
{ //监听应用属性改变的事件
public void
attributeAdded(ServletContextAttributeEvent arg0) {
System.out.println("增加一个ServletContext属性:attributeAdded('"+arg0.getName()+"','"+arg0.getValue()+"')");
}
public void
attributeRemoved(ServletContextAttributeEvent arg0) {
System.out.println("删除了一个ServletContext属性:
attributeRemoved('"+arg0.getName()+"','"+arg0.getValue()+"')");
}
public void
attributeReplaced(ServletContextAttributeEvent arg0) {
System.out.println("某个ServletContext的属性被改变:
attributeReplaced('"+arg0.getName()+"','"+arg0.getValue()+"')");
}
}
package com.yd.listener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MyHttpSessionListener implements
HttpSessionListener {
//创建和销毁session是触发监听接口
public void sessionCreated(HttpSessionEvent arg0)
{
Integer in = null;
in = (Integer)
arg0.getSession().getServletContext().getAttribute("SESSION_COUNT");
if(in != null){
arg0.getSession().getServletContext().setAttribute("SESSION_COUNT",
new Integer(in.intValue()+1));
}else{
in = new
Integer(1);
arg0.getSession().getServletContext().setAttribute("SESSION_COUNT",
new Integer(in.intValue()));
}
}
public void sessionDestroyed(HttpSessionEvent
arg0) {
Integer in = null;
in = (Integer)
arg0.getSession().getServletContext().getAttribute("SESSION_COUNT");
if(in != null){
arg0.getSession().getServletContext().setAttribute("SESSION_COUNT",
new Integer(in.intValue()-1));
}else{
in = new
Integer(0);
arg0.getSession().getServletContext().setAttribute("SESSION_COUNT",
new Integer(in.intValue()));
}
}
}
encoding="UTF-8"?> //配置文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
com.yd.listener.ServletRequeseList
com.yd.listener.MyServletContextAttributeListener
com.yd.listener.MyHttpSessionListener
This is the description
of my J2EE
component
This is the display
name of my J2EE
component
TestServlet
com.yd.listener.TestServlet
TestServlet
/TestServlet
//浏览器页面
import="java.util.*,com.yd.listener.*"
pageEncoding="utf-8"%>
String path = request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
/p>
Transitional//EN">
href="">
My JSP 'index.jsp' startingpage
content="no-cache">
content="no-cache">
content="0">
content="keyword1,keyword2,keyword3">
content="This is my page">
request.getSession().setMaxInactiveInterval(6);
request.getSession().setAttribute("MyShopCar",
new MyShopCar());
%>
登录过的人数有(request):
color
="red">${REQUESE_COUNT}
用session统计在线人数:
color =
"red">${SESSION_COUNT}
用binding统计人数:
color = "red">${BINDING_COUNT
}