listener 监听 tomcat 容器的初始化和销毁
为了简单,就写个统计Action 请求数量的例子:
1、首先写个 listener
public class TestServletContextListener implements ServletContextListener
{ @Override
public void contextDestroyed(ServletContextEvent e)
{
System.out.println("--event-- context destoryed..");
e.getServletContext().log("ssseeevvv");
System.out.println(e.getServletContext().getAttribute("count")); // System.out.println(ActionContext.getContext().getApplication().get("count")); //获取不到 ActionContext
} @Override
public void contextInitialized(ServletContextEvent evt)
{
System.out.println("--event-- context initialized..");
ServletContext ctx = evt.getServletContext(); if(null == ctx.getAttribute("count"))
{
22 ctx.setAttribute("count",0); //为了简单起见,我就数数了
//ctx.setAttribute("count", new HashMap<String,Set<String>>());
}
} }
2、在拦截器中获取信息,保存在 ServletContext 的 count 对象里。
public class TestInterceptor extends MethodFilterInterceptor
{
private static final long serialVersionUID = 1L; @Override
protected String doIntercept(ActionInvocation invocation) throws Exception
{
System.out.println("-m- Enter into TestInterceptor...");
countActionRequestNumber(); invocation.invoke(); //一定要有这一句,不然请求走到这个拦截器之后就断了
return null;
} private void countActionRequestNumber()
{
//方式二: Spring 的listener是从ServletContext中获取的 getInitParameter
ServletContext servletContext = ServletActionContext.getServletContext();
String parameter = servletContext.getInitParameter("test"); if(Boolean.valueOf(parameter))
{
int count = (int)servletContext.getAttribute("count");
servletContext.setAttribute("count", ++count);
}
}
}
3、在 web.xml 中注册监听器
<context-param>
<param-name>test</param-name>
<param-value>true</param-value>
</context-param> <listener>
<listener-class>test.listener.TestServletContextListener</listener-class>
</listener>
4、在struts 文件中配置拦截器
<package name="one" extends="struts-default">
<interceptors>
<interceptor name="test" class="test.interceptor.TestInterceptor"></interceptor>
<interceptor-stack name="selfDefaultIntcptorStack">
<interceptor-ref name="test"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="selfDefaultIntcptorStack"></default-interceptor-ref>
<action name="test1" class="test.action.TestAction" method="test1">
</action>
<action name="test2" class="test.action.TestAction" method="test2">
</action>
</package>
note: 如果是 eclipse 启动的 tomcat ,最好是使用右键 stopped 停止服务器,而不是直接点击红色按钮强制关闭。
listener 监听 tomcat 容器的初始化和销毁的更多相关文章
- 监听tomcat服务器启动/关闭并从配置文件中读取参数进行初始化
监听tomcat服务器启动/关闭很简单(2步): 1. 建立一个类实现ServletContextListener接口,重写其中的方法(contextDestroyed和contextInitiali ...
- 监听Web容器启动与关闭
在Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期. 要监听web ...
- javaWeb学习之Listener监听
] 一.监听器Listener javaEE包括13门规范 在课程中主要学习 servlet技术 和 jsp技术 其中 servlet规范包括三个技术点:servlet listener filt ...
- java多个listener监听
java 多个listener 监听方法 在class 名称上一行添加@Listeners 括号中用逗号隔开 @Listeners({com.example.MyListener.class,com. ...
- Listener 监听Session内的对象
Listener用于监控Session内的对象,分别是HttpSessionBindingListener与HttpSessionActivationListener.它们的触发时机分别为: Http ...
- ServletContextListener使用详解(监听Tomcat启动、关闭)
在 Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期. 当Serv ...
- Listener 监听对象的创建和销毁
HttpSessionListener.ServletContextListener.ServletRequestListener分别用于控制Session.context.request的创建和销毁 ...
- Android Listener 监听的几种写法
Android中,View的Listener方法,在是否使用匿名类匿名对象时,有各种不同的写法. OnClickListener和其他Listener方法一样,都是View类的接口,重载实现后就能使用 ...
- 怎样绕过oracle listener 监听的password设置
怎样绕过oracle 监听的password设置: 1.找到监听进程pid ,并将它kill 掉 ps -ef|grep tns [oracle@lixora admin]$ ps -ef|gr ...
随机推荐
- H5实现全屏与F11全屏
最近做项目用到全屏,现总结一下全屏: 1.局部全屏:H5全屏和F11有区别,在这种情况下判断全屏只需要通过H5全屏属性,无论全屏后有无滚动条都可判断. /** * [isFullscreen 判断浏览 ...
- 2017-2018 Northwestern European Regional Contest (NWERC 2017)
A. Ascending Photo 贪心增广. #include<bits/stdc++.h> using namespace std; const int MAXN = 1000000 ...
- 创建phpinfo(PHP探针)查看自己服务器空间php详细信息
创建phpinfo(PHP探针)查看自己服务器空间php详细信息 <?phpphpinfo();?> 保存,然后更改文件名为phpinfo.php 放到你域名根目录,然后访问:http:/ ...
- c#常用数值范围汇总
short.MaxValue 32767 short.MinValue -32768 int.MaxValue 2147483647 int.MinValue -2147483648 long.Max ...
- Linux 安装zookeeper
分享到: 1.下载zokeeper 1.1 官网下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/ 1.2 链接:https:/ ...
- timesten 修改最大连接数
修改完/var/Timesten/sys.odbc.ini里面的connections之后 重启TT:ttdaemonadmin -restart 报错:15019: Only the instanc ...
- PropertyEditor、Formatter、Converter的应用
@ResponseBody @RequestMapping("date.do") public String data(Date date) { return date.toStr ...
- HTML列表与表格
border:控制边框 width:宽度 height:高度 table是表格 tr:行 td:列★ colspan:合并列★rowspan:合并行★ <!doctype html> &l ...
- 前言|Elena
2019.3.19更新置顶 2018.11.5更新置顶 2018.9.7更新置顶 -这里写下置顶- 嗨嗨嗨 这里AlenaNuna,偏远小渔村oi蒟蒻一只,各大题库id有Elena/AlenaNuna ...
- mysql 储存过程
存储过程如同一门程序设计语言,同样包含了数据类型.流程控制.输入和输出和它自己的函数库. --------------------基本语法-------------------- 一.创建存储过程cr ...