java session事件,Servlet (session,request等)事件监听

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' starting

page

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值