{"id":630,"date":"2015-05-06T10:22:28","date_gmt":"2015-05-06T02:22:28","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=630"},"modified":"2016-12-02T10:13:59","modified_gmt":"2016-12-02T02:13:59","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e7%9b%91%e5%90%ac%e5%99%a8servlet","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=630","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u76d1\u542c\u5668Servlet"},"content":{"rendered":"<h2>\u4e00\u3001\u76d1\u542c\u5668\u6982\u8ff0<\/h2>\n<p>1.Listener\u662fServlet\u7684\u76d1\u542c\u5668<\/p>\n<p>2.\u53ef\u4ee5\u76d1\u542cServletContext\u3001HttpSession\u548cServletRequest\u7b49\u57df\u5bf9\u8c61\u7684\u521b\u5efa\u4e0e\u9500\u6bc1\u4e8b\u4ef6,\u57df\u5bf9\u8c61\u5c5e\u6027\u4fee\u6539\u4e8b\u4ef6\uff0c\u5728\u4e8b\u4ef6\u53d1\u751f\u524d\u3001\u53d1\u751f\u540e\u505a\u4e00\u4e9b\u5fc5\u8981\u7684\u5904\u7406.<br \/>\n<!--more--><\/p>\n<p>3.\u901a\u8fc7\u76d1\u542c\u5668\uff0c\u53ef\u4ee5\u81ea\u52a8\u6fc0\u53d1\u4e00\u4e9b\u64cd\u4f5c\uff0c\u5982\u76d1\u542c\u5728\u7ebf\u7528\u6237\u6570\u91cf\uff0c\u5f53\u589e\u52a0\u4e00\u4e2aHttpSession\u65f6\uff0c\u7ed9\u5728\u7ebf\u4eba\u6570\u52a01\u3002<\/p>\n<h2>\u4e8c\u3001\u5e38\u7528\u7684\u76d1\u542c\u63a5\u53e3<\/h2>\n<h3>\u00a01 .\u76d1\u542c\u57df\u5bf9\u8c61\u7684\u521b\u5efa\u4e0e\u9500\u6bc1\u4e8b\u4ef6<\/h3>\n<p>1.ServletContextListener<\/p>\n<p>\u76d1\u542cServletContext\uff0c\u5f53\u521b\u5efaServletContext\u65f6\uff0c\u6fc0\u53d1contextInitialized(ServletContextEvent sce)\u65b9\u6cd5\uff1b\u5f53\u9500\u6bc1ServletContext\u65f6\uff0c\u6fc0\u53d1contextDestroyed(ServletContextEvent sce)\u65b9\u6cd5\u3002<br \/>\n<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/05\/QQ\u255c\u256a\u2550\u255d20161129211116.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1777\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/05\/QQ\u255c\u256a\u2550\u255d20161129211116.png\" alt=\"QQ\u255c\u256a\u2550\u255d20161129211116\" width=\"633\" height=\"371\" \/><\/a><\/p>\n<p>2.HttpSessionListener<\/p>\n<p>\u76d1\u542cHttpSession\u7684\u64cd\u4f5c\u3002\u5f53\u521b\u5efa\u4e00\u4e2aSession\u65f6\uff0c\u6fc0\u53d1session Created(SessionEvent se)\u65b9\u6cd5\uff1b\u5f53\u9500\u6bc1\u4e00\u4e2aSession\u65f6\uff0c\u6fc0\u53d1sessionDestroyed (HttpSessionEvent se)\u65b9\u6cd5\u3002<\/p>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/05\/QQ\u255c\u256a\u2550\u255d20161129211513.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1779\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/05\/QQ\u255c\u256a\u2550\u255d20161129211513.png\" alt=\"QQ\u255c\u256a\u2550\u255d20161129211513\" width=\"648\" height=\"371\" \/><\/a><\/p>\n<p>3.ServletRequestListener<br \/>\n\u76d1\u542c\u8bf7\u6c42\u6d88\u606f\u5bf9\u8c61(ServletRequest)\u7684\u4e8b\u4ef6<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1778\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/05\/QQ\u255c\u256a\u2550\u255d20161129211437.png\" alt=\"QQ\u255c\u256a\u2550\u255d20161129211437\" width=\"636\" height=\"398\" \/><\/p>\n<h3>2.\u00a0\u76d1\u542c\u57df\u5bf9\u8c61\u5c5e\u6027\u589e\u52a0\u548c\u5220\u9664\u4e8b\u4ef6<\/h3>\n<p>1.ServletContextAttributeListener<br \/>\n\u76d1\u542c\u5bf9ServletContext\u5c5e\u6027\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u589e\u52a0\/\u5220\u9664\/\u4fee\u6539<\/p>\n<p>2.HttpSessionAttributeListener<br \/>\n\u76d1\u542cHttpSession\u4e2d\u7684\u5c5e\u6027\u7684\u64cd\u4f5c\u3002\u5f53\u5728Session\u589e\u52a0\u4e00\u4e2a\u5c5e\u6027\u65f6\uff0c\u6fc0\u53d1attributeAdded(HttpSessionBindingEvent se) \u65b9\u6cd5\uff1b\u5f53\u5728Session\u5220\u9664\u4e00\u4e2a\u5c5e\u6027\u65f6\uff0c\u6fc0\u53d1attributeRemoved(HttpSessionBindingEvent se)\u65b9\u6cd5\uff1b\u5f53\u5728Session\u5c5e\u6027\u88ab\u91cd\u65b0\u8bbe\u7f6e\u65f6\uff0c\u6fc0\u53d1attributeReplaced(HttpSessionBindingEvent se) \u65b9\u6cd5\u3002<\/p>\n<p>3.ServletRequestAttributeListener<br \/>\n\u76d1\u542cServletRequest\u4e2d\u7684\u5c5e\u6027\u7684\u64cd\u4f5c<br \/>\n<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/05\/QQ\u255c\u256a\u2550\u255d20161129211403.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1780\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/05\/QQ\u255c\u256a\u2550\u255d20161129211403.png\" alt=\"QQ\u255c\u256a\u2550\u255d20161129211403\" width=\"631\" height=\"333\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2>\u4e09\u3001\u4f7f\u7528\u8303\u4f8b<\/h2>\n<h3>1\u3001\u7531\u76d1\u542c\u5668\u7ba1\u7406\u5171\u4eab\u6570\u636e\u5e93\u8fde\u63a5<\/h3>\n<p>\u751f\u547d\u5468\u671f\u4e8b\u4ef6\u7684\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u7531context\u76d1\u542c\u5668\u7ba1\u7406\u5171\u4eab\u6570\u636e\u5e93\u8fde\u63a5\u3002\u5728web.xml\u4e2d\u5982\u4e0b\u5b9a\u4e49\u76d1\u542c\u5668\uff1a<\/p>\n<p>[java]&lt;listener&gt;<br \/>\n    &lt;listener-class&gt;XXX.MyConnectionManager&lt;\/listener-class&gt;<br \/>\n&lt;\/listener&gt;server[\/java]<\/p>\n<p>\u521b\u5efa\u76d1\u542c\u5668\u7684\u5b9e\u4f8b\uff0c\u63a5\u53d7\u4e8b\u4ef6\u5e76\u81ea\u52a8\u5224\u65ad\u5b9e\u73b0\u76d1\u542c\u5668\u63a5\u53e3\u7684\u7c7b\u578b\u3002\u8981\u8bb0\u4f4f\u7684\u662f\u7531\u4e8e\u76d1\u542c\u5668\u662f\u914d\u7f6e\u5728\u90e8\u7f72\u63cf\u8ff0\u7b26web.xml\u4e2d\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u6539\u53d8\u4efb\u4f55\u4ee3\u7801\u5c31\u53ef\u4ee5\u6dfb\u52a0\u65b0\u7684\u76d1\u542c\u5668\u3002<\/p>\n<p>[java]<br \/>\npublic class MyConnectionManager implements ServletContextListener{<br \/>\n   public void contextInitialized(ServletContextEvent e) {<br \/>\n       Connection con = \/\/ create connection<br \/>\n       e.getServletContext().setAttribute(&quot;con&quot;, con);<br \/>\n   }<br \/>\n   public void contextDestroyed(ServletContextEvent e) {<br \/>\n       Connection con = (Connection) e.getServletContext().getAttribute(&quot;con&quot;);<br \/>\n       try {<br \/>\n         con.close();<br \/>\n       }<br \/>\n       catch (SQLException ignored) { } \/\/ close connection<br \/>\n   }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>\u76d1\u542c\u5668\u4fdd\u8bc1\u6bcf\u65b0\u751f\u6210\u4e00\u4e2aservlet context\u90fd\u4f1a\u6709\u4e00\u4e2a\u53ef\u7528\u7684\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5e76\u4e14\u6240\u6709\u7684\u8fde\u63a5\u5bf9\u4f1a\u5728context\u5173\u95ed\u7684\u65f6\u5019\u968f\u4e4b\u5173\u95ed\u3002<\/p>\n<h3>2\u3001\u8ba1\u7b97\u5728\u7ebf\u7528\u6237\u6570\u91cf\u7684Linstener<\/h3>\n<p>(1)<\/p>\n<p>[java]Package xxx; <\/p>\n<p>public class OnlineCounter {<br \/>\n  private static long online = 0;<br \/>\n  public static long getOnline(){<br \/>\n    return online;<br \/>\n  }<br \/>\n  public static void raise(){<br \/>\n    online++;<br \/>\n  }<br \/>\n  public static void reduce(){<br \/>\n    online&#8211;;<br \/>\n  }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>[java]import javax.servlet.http.HttpSessionEvent;<br \/>\nimport javax.servlet.http.HttpSessionListener;<\/p>\n<p>public class OnlineCounterListener implements HttpSessionListener{<br \/>\n  public void sessionCreated(HttpSessionEvent hse) {<br \/>\n     OnlineCounter.raise();<br \/>\n  }<br \/>\n  public void sessionDestroyed(HttpSessionEvent hse){<br \/>\n     OnlineCounter.reduce();<br \/>\n  }<br \/>\n}[\/java]<\/p>\n<p>\u5728\u9700\u8981\u663e\u793a\u5728\u7ebf\u4eba\u6570\u7684JSP\u4e2d\u53ef\u662f\u4f7f\u7528<\/p>\n<p>\u76ee\u524d\u5728\u7ebf\u4eba\u6570\uff1a<\/p>\n<p>[java]<br \/>\n&lt;%@ page import=\u201cxxx.OnlineCounter&quot; %&gt;<br \/>\n&lt;%=OnlineCounter.getOnline()%&gt;<br \/>\n [\/java]<\/p>\n<p>\u9000\u51fa\u4f1a\u8bdd(\u53ef\u4ee5\u7ed9\u7528\u6237\u63d0\u4f9b\u4e00\u4e2a\u6ce8\u9500\u6309\u94ae)\uff1a<\/p>\n<p>[java]&lt;form action=&quot;exit.jsp&quot; method=post&gt;<br \/>\n  &lt;input type=submit value=&quot;exit&quot;&gt;<br \/>\n&lt;\/form&gt; <\/p>\n<p>exit.jsp: &lt;%session.invalidate() ;%&gt;<\/p>\n<p>[\/java]<\/p>\n<p>\u5728web.xml\u4e2d\u52a0\u5165:<\/p>\n<p>[java]<br \/>\n&lt;listener&gt;<br \/>\n &lt;listener-class&gt;servletlistener111111.SecondListener&lt;\/listener-class&gt; &lt;\/listener&gt;<\/p>\n<p>[\/java]<\/p>\n<p>\u600e\u4e48\u6837\uff0c\u5c31\u662f\u8fd9\u4e48\u7b80\u5355\uff0c\u4e0d\u7528\u5bf9\u73b0\u6709\u4ee3\u7801\u505a\u4efb\u4f55\u7684\u4fee\u6539\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u76d1\u542c\u5668\u6982\u8ff0 1.Listener\u662fServlet\u7684\u76d1\u542c\u5668 2.\u53ef\u4ee5\u76d1\u542cServletContext\u3001Htt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[60],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/630"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=630"}],"version-history":[{"count":4,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/630\/revisions"}],"predecessor-version":[{"id":1781,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/630\/revisions\/1781"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}