Servlet程序由Servlet,Filter和Listener组成,其中监听器用来监听Servlet容器上下文。
监听器通常分三类:基于Servlet上下文的ServletContex监听,基于会话的HttpSession监听和基于请求的ServletRequest监听。
/** * Application监听器,Servlet中的Application即ServletContext * @author Administrator */ public class ApplicationListener implements ServletContextListener, ServletContextAttributeListener { /** * application销毁时触发的事件 */ @Override public void contextDestroyed(ServletContextEvent arg0) { System.out.println("Application销毁:"+arg0.getServletContext()); } /** * application初始化时触发的方法 */ @Override public void contextInitialized(ServletContextEvent arg0) { System.out.println("Application创建:"+arg0.getServletContext()); } /** * application中添加属性值时触发的方法 */ @Override public void attributeAdded(ServletContextAttributeEvent arg0) { System.out.println("Application添加新属性:key="+arg0.getName()+" value="+arg0.getValue()); } /** * application中删除属性值时触发的方法 */ @Override public void attributeRemoved(ServletContextAttributeEvent arg0) { System.out.println("Application移除属性:key="+arg0.getName()+" value="+arg0.getValue()); } /** * application中替换属性值时触发的方法 */ @Override public void attributeReplaced(ServletContextAttributeEvent arg0) { System.out.println("Application替换属性:key="+arg0.getName()+" value="+arg0.getValue()); } }
最后在web.xml需要注册监听器,注册方式非常简单,注意标签<description>和<display-name>不是必须的:
<listener> <description>application listener</description> <display-name>application_listener</display-name> <listener-class>com.bless.listener.application.ApplicationListener</listener-class> </listener>
随后启动java web项目,监听器就会运行。
/** * Session监听器 * @author Administrator */ public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener { Vector<HttpSession> listSession = null; /** * 创建Session调用的方法 * 将session对象放入listSession集合中 */ @Override public void sessionCreated(HttpSessionEvent arg0) { synchronized (this) { if(listSession == null){ listSession = new Vector<HttpSession>(); } } listSession.add(arg0.getSession()); System.out.println("\n\n创建一个Session:"+arg0.getSession()); System.out.println("[当前存在的Session:]"); for (HttpSession session : listSession) { System.out.println("--->"+session); } } /** * 销毁Session调用的方法 * 移除listSession集合对应session值 */ @Override public void sessionDestroyed(HttpSessionEvent arg0) { listSession.remove(arg0.getSession()); System.out.println("\n\n销毁一个Session:"+arg0.getSession()); System.out.println("[当前存在的Session:]"); for (HttpSession session : listSession) { System.out.println("--->"+session); } } /** * session属性添加时调用的方法 */ @Override public void attributeAdded(HttpSessionBindingEvent arg0) { System.out.println("\n\n添加一条Session-->key:"+arg0.getName()+" 属性value:"+arg0.getValue()); } /** * session属性移除时调用的方法 */ @Override public void attributeRemoved(HttpSessionBindingEvent arg0) { System.out.println("\n\n删除一条Session-->key:"+arg0.getName()+" 属性value:"+arg0.getValue()); } /** * session属性替代时调用的方法 */ @Override public void attributeReplaced(HttpSessionBindingEvent arg0) { System.out.println("\n\n覆盖一条Session-->key:"+arg0.getName()+" 属性value:"+arg0.getValue()); } }
在web.xml中定义相应监听器配置:
<listener> <listener-class>com.bless.listener.session.SessionListener</listener-class> </listener> <!-- Session超时配置 --> <session-config> <session-timeout>1</session-timeout> </session-config>
/** * Request事件监听器 * @author Administrator */ public class RequestListener implements ServletRequestListener, ServletRequestAttributeListener { @Override public void requestDestroyed(ServletRequestEvent arg0) { System.out.println("request销毁:"+arg0.getServletRequest()); } @Override public void requestInitialized(ServletRequestEvent arg0) { System.out.println("request创建:"+arg0.getServletRequest()); } @Override public void attributeAdded(ServletRequestAttributeEvent arg0) { System.out.println("request属性添加 key="+arg0.getName()+" value="+arg0.getValue()); } @Override public void attributeRemoved(ServletRequestAttributeEvent arg0) { System.out.println("request属性删除 key="+arg0.getName()+" value="+arg0.getValue()); } @Override public void attributeReplaced(ServletRequestAttributeEvent arg0) { System.out.println("request属性替换 key="+arg0.getName()+" value="+arg0.getValue()); } }
<listener> <listener-class>com.bless.listener.request.RequestListener</listener-class> </listener>
/** * * @author : bless<[email protected]> * Create Time : 2011-5-10下午10:38:19 * Description : 字符集格式过滤器 * */ public class EncodingFilter implements Filter { //默认编码格式UTF-8 private static final String DEFAULT_ENCODE = "UTF-8"; private String encodeName; // 编码格式 public void destroy() { } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { if (encodeName == null || "".equals(encodeName.trim())) { request.setCharacterEncoding(DEFAULT_ENCODE); response.setCharacterEncoding(DEFAULT_ENCODE); } else { request.setCharacterEncoding(encodeName); } } catch (UnsupportedEncodingException e) { throw new UnsupportedEncodingException("编码格式过滤错误,请确认web.xml填入了正确的编码格式"); } chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { //获取web.xml配置的<param-name>encodeName</param-name>的值 this.setEncodeName(fConfig.getInitParameter("encodeName")); } public String getEncodeName() { return encodeName; } public void setEncodeName(String encodeName) { this.encodeName = encodeName; } }
<filter> <filter-name>encoding</filter-name> <filter-class>com.mt.filter.EncodingFilter</filter-class> <init-param> <param-name>encodeName</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>