{"id":630,"date":"2015-05-06T10:22:28","date_gmt":"2015-05-06T02:22:28","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=630"},"modified":"2026-08-18T11:17:44","modified_gmt":"2026-08-18T03:17:44","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":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">Listener<\/strong> \u662f Servlet \u89c4\u8303\u91cc\u7684\u76d1\u542c\u5668\u3002\u5b83\u53ef\u4ee5\u76d1\u542c <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">ServletContext<\/code>\u3001<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">HttpSession<\/code>\u3001<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">ServletRequest<\/code> \u7b49\u57df\u5bf9\u8c61\u7684\u521b\u5efa\u4e0e\u9500\u6bc1\uff0c\u4ee5\u53ca\u5c5e\u6027\u589e\u5220\u6539\uff0c\u5728\u4e8b\u4ef6\u524d\u540e\u505a\u5fc5\u8981\u5904\u7406\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5178\u578b\u7528\u6cd5\u662f\u7edf\u8ba1\u5728\u7ebf\u4eba\u6570\uff1a\u65b0\u5efa\u4e00\u4e2a HttpSession \u5c31\u52a0\u4e00\uff0c\u9500\u6bc1\u5c31\u51cf\u4e00\u3002\u76d1\u542c\u5668\u5199\u5728 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">web.xml<\/code> \u91cc\uff0c\u4e0d\u5fc5\u6539\u73b0\u6709\u4e1a\u52a1\u4ee3\u7801\u5c31\u80fd\u6302\u4e0a\u3002<\/p>\n<p><!--more--><\/p>\n<p style=\"margin: 1em 0px;\">\u63a5\u53e3\u5206\u6210\u4e24\u7c7b\uff1a\u76d1\u542c\u57df\u5bf9\u8c61\u672c\u8eab\u7684\u751f\u706d\uff0c\u4ee5\u53ca\u76d1\u542c\u57df\u5bf9\u8c61\u4e0a\u7684\u5c5e\u6027\u53d8\u5316\u3002\u5bb9\u5668\u4f1a\u6839\u636e\u4f60\u5b9e\u73b0\u7684\u63a5\u53e3\u81ea\u52a8\u56de\u8c03\u5bf9\u5e94\u65b9\u6cd5\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u76d1\u542c\u5668\u6982\u8ff0<\/h2>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>Listener \u662f Servlet \u7684\u76d1\u542c\u5668\u3002<\/section>\n<\/li>\n<li>\n<section>\u53ef\u76d1\u542c Context \/ Session \/ Request \u7684\u521b\u5efa\u4e0e\u9500\u6bc1\uff0c\u4ee5\u53ca\u5c5e\u6027\u4fee\u6539\u3002<\/section>\n<\/li>\n<li>\n<section>\u4e8b\u4ef6\u53d1\u751f\u65f6\u81ea\u52a8\u6fc0\u53d1\u64cd\u4f5c\uff0c\u4f8b\u5982\u5728\u7ebf\u7528\u6237\u8ba1\u6570\u3002<\/section>\n<\/li>\n<\/ul>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u5e38\u7528\u7684\u76d1\u542c\u63a5\u53e3<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">1. \u76d1\u542c\u57df\u5bf9\u8c61\u7684\u521b\u5efa\u4e0e\u9500\u6bc1<\/h3>\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">ServletContextListener<\/strong>\uff1a\u521b\u5efa\u65f6\u56de\u8c03 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">contextInitialized<\/code>\uff0c\u9500\u6bc1\u65f6\u56de\u8c03 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">contextDestroyed<\/code>\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><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 style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">HttpSessionListener<\/strong>\uff1a\u521b\u5efa Session \u65f6\u56de\u8c03 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">sessionCreated<\/code>\uff0c\u9500\u6bc1\u65f6\u56de\u8c03 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">sessionDestroyed<\/code>\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><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 style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">ServletRequestListener<\/strong>\uff1a\u76d1\u542c\u8bf7\u6c42\u5bf9\u8c61 ServletRequest \u7684\u521b\u5efa\u4e0e\u9500\u6bc1\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><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 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">2. \u76d1\u542c\u57df\u5bf9\u8c61\u5c5e\u6027\u7684\u589e\u52a0\u548c\u5220\u9664<\/h3>\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">ServletContextAttributeListener<\/strong>\uff1a\u76d1\u542c\u5bf9 ServletContext \u5c5e\u6027\u7684\u589e\u52a0 \/ \u5220\u9664 \/ \u4fee\u6539\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">HttpSessionAttributeListener<\/strong>\uff1aSession \u589e\u52a0\u5c5e\u6027\u65f6 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">attributeAdded<\/code>\uff0c\u5220\u9664\u65f6 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">attributeRemoved<\/code>\uff0c\u8986\u76d6\u65f6 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">attributeReplaced<\/code>\uff0c\u4e8b\u4ef6\u7c7b\u578b\u662f <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">HttpSessionBindingEvent<\/code>\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">ServletRequestAttributeListener<\/strong>\uff1a\u76d1\u542c ServletRequest \u4e2d\u7684\u5c5e\u6027\u64cd\u4f5c\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><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<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u63a5\u53e3<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u76d1\u542c\u5bf9\u8c61<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u5173\u952e\u56de\u8c03<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">ServletContextListener<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5e94\u7528\u4e0a\u4e0b\u6587<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">contextInitialized \/ contextDestroyed<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">HttpSessionListener<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4f1a\u8bdd<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">sessionCreated \/ sessionDestroyed<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">ServletRequestListener<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4e00\u6b21\u8bf7\u6c42<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">requestInitialized \/ requestDestroyed<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">ServletContextAttributeListener<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Context \u5c5e\u6027<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">added \/ removed \/ replaced<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">HttpSessionAttributeListener<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Session \u5c5e\u6027<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">added \/ removed \/ replaced<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">ServletRequestAttributeListener<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Request \u5c5e\u6027<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">added \/ removed \/ replaced<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u7531\u76d1\u542c\u5668\u7ba1\u7406\u5171\u4eab\u6570\u636e\u5e93\u8fde\u63a5<\/h2>\n<p style=\"margin: 1em 0px;\">\u751f\u547d\u5468\u671f\u4e8b\u4ef6\u7684\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\uff1a\u7528 Context \u76d1\u542c\u5668\u7ba1\u7406\u5171\u4eab\u6570\u636e\u5e93\u8fde\u63a5\u3002\u5728 web.xml \u4e2d\u58f0\u660e\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;listener&gt;\n    &lt;listener-class&gt;XXX.MyConnectionManager&lt;\/listener-class&gt;\n&lt;\/listener&gt;<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5bb9\u5668\u4f1a\u521b\u5efa\u76d1\u542c\u5668\u5b9e\u4f8b\uff0c\u6839\u636e\u5b9e\u73b0\u7684\u63a5\u53e3\u7c7b\u578b\u5206\u53d1\u4e8b\u4ef6\u3002\u76d1\u542c\u5668\u914d\u5728\u90e8\u7f72\u63cf\u8ff0\u7b26\u91cc\uff0c\u6dfb\u52a0\u65b0\u76d1\u542c\u5668\u4e0d\u5fc5\u6539\u4e1a\u52a1\u4ee3\u7801\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class MyConnectionManager implements ServletContextListener {\n    public void contextInitialized(ServletContextEvent e) {\n        Connection con = null; \/\/ create connection\n        e.getServletContext().setAttribute(\"con\", con);\n    }\n\n    public void contextDestroyed(ServletContextEvent e) {\n        Connection con = (Connection) e.getServletContext().getAttribute(\"con\");\n        try {\n            con.close();\n        } catch (SQLException ignored) {\n        }\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8fd9\u6837\u6bcf\u4e2a\u65b0\u751f\u6210\u7684 ServletContext \u90fd\u6709\u53ef\u7528\u8fde\u63a5\uff0cContext \u5173\u95ed\u65f6\u8fde\u63a5\u968f\u4e4b\u5173\u95ed\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u56db\u3001\u8ba1\u7b97\u5728\u7ebf\u7528\u6237\u6570\u91cf<\/h2>\n<p style=\"margin: 1em 0px;\">\u7528\u4e00\u4e2a\u8ba1\u6570\u5668\u548c Session \u76d1\u542c\u5668\u914d\u5408\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">package xxx;\n\npublic class OnlineCounter {\n    private static long online = 0;\n\n    public static long getOnline() {\n        return online;\n    }\n\n    public static void raise() {\n        online++;\n    }\n\n    public static void reduce() {\n        online--;\n    }\n}<\/code><\/pre>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">import javax.servlet.http.HttpSessionEvent;\nimport javax.servlet.http.HttpSessionListener;\n\npublic class OnlineCounterListener implements HttpSessionListener {\n    public void sessionCreated(HttpSessionEvent hse) {\n        OnlineCounter.raise();\n    }\n\n    public void sessionDestroyed(HttpSessionEvent hse) {\n        OnlineCounter.reduce();\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u9700\u8981\u663e\u793a\u5728\u7ebf\u4eba\u6570\u7684 JSP\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;%@ page import=\"xxx.OnlineCounter\" %&gt;\n\u76ee\u524d\u5728\u7ebf\u4eba\u6570\uff1a&lt;%= OnlineCounter.getOnline() %&gt;<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u7ed9\u7528\u6237\u4e00\u4e2a\u6ce8\u9500\u6309\u94ae\uff0c\u5728 exit.jsp \u91cc\u8ba9\u4f1a\u8bdd\u5931\u6548\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;form action=\"exit.jsp\" method=\"post\"&gt;\n  &lt;input type=\"submit\" value=\"exit\"&gt;\n&lt;\/form&gt;\n\n&lt;%-- exit.jsp --%&gt;\n&lt;% session.invalidate(); %&gt;<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5728 web.xml \u4e2d\u52a0\u5165\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;listener&gt;\n    &lt;listener-class&gt;servletlistener111111.SecondListener&lt;\/listener-class&gt;\n&lt;\/listener&gt;<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u4e0d\u7528\u5bf9\u73b0\u6709\u4e1a\u52a1\u4ee3\u7801\u505a\u4fee\u6539\uff0c\u53ea\u8981\u628a\u76d1\u542c\u5668\u6302\u4e0a\u5373\u53ef\u3002<\/p>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u76d1\u542c\u5668\u6302\u5728 web.xml \u91cc\uff1a\u5bb9\u5668\u5728 Context \/ Session \/ Request \u751f\u706d\u65f6\u56de\u8c03\uff0c\u9002\u5408\u653e\u5171\u4eab\u8d44\u6e90\u548c\u8ba1\u6570\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>Listener \u662f Servlet \u89c4\u8303\u91cc\u7684\u76d1\u542c\u5668\u3002\u5b83\u53ef\u4ee5\u76d1\u542c ServletContext\u3001HttpSes [&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":[183,182],"_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":3,"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}]}}