Servlet程序由Servlet,Filter和Listener組成,其中監聽器用來監聽Servlet容器上下文。java
監聽器一般分三類:基於Servlet上下文的ServletContex監聽,基於會話的HttpSession監聽和基於請求的ServletRequest監聽。web
/** * 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集合中 */ 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值 */ 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屬性添加時調用的方法 */ public void attributeAdded(HttpSessionBindingEvent arg0) { System.out.println("\n\n添加一條Session-->key:"+arg0.getName()+" 屬性value:"+arg0.getValue()); } /** * session屬性替代時調用的方法 */ public void attributeReplaced(HttpSessionBindingEvent arg0) { System.out.println("\n\n覆蓋一條Session-->key:"+arg0.getName()+" 屬性value:"+arg0.getValue()); } /** * session屬性移除時調用的方法 */ public void attributeRemoved(HttpSessionBindingEvent arg0) { System.out.println("\n\n刪除一條Session-->key:"+arg0.getName()+" 屬性value:"+arg0.getValue()); } }
在web.xml中定義相應監聽器配置:session
<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()); } }
web.xml配置:app
<listener> <listener-class>com.bless.listener.request.RequestListener</listener-class> </listener>
/** * * @author : bless<505629625@qq.com> * 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; } }
而後在web.xml中定義filter便可,標籤init-param能夠作一個參數配置,在filter中經過init方法參數FilterConfig.getInitParameter得到:less
<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>
轉自:http://blessht.iteye.com/blog/1164492ssh
http://www.iteye.com/topic/483158ide
http://chinaxxren.iteye.com/blog/811604性能
http://blog.csdn.net/fyxxq/article/details/9731747
http://ooft.iteye.com/blog/551498
http://www.iteye.com/topic/82565