{"id":1791,"date":"2016-12-02T11:01:42","date_gmt":"2016-12-02T03:01:42","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1791"},"modified":"2016-12-02T11:03:41","modified_gmt":"2016-12-02T03:03:41","slug":"web%e8%bf%87%e8%99%91%e5%99%a8","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1791","title":{"rendered":"web\u8fc7\u8651\u5668"},"content":{"rendered":"<h3>\u4e00\u3001\u8fc7\u8651\u5668\u5b9a\u4e49<\/h3>\n<p>\u8fc7\u8651\u5668\u662f\u4e00\u4e2a\u670d\u52a1\u7aef\u5b9e\u73b0javax.servlet.Filter\u63a5\u53e3\u7684java\u7c7b\uff0c\u5b83\u53ef\u4ee5\u622a\u53d6\u7528\u6237\u7aef\u7684\u8bf7\u6c42\u4e0e\u670d\u52a1\u7aef\u7684\u54cd\u5e94\u4fe1\u606f\uff0c\u5b9e\u73b0\u5bf9\u8fd9\u4e9b\u4fe1\u606f\u8fc7\u8651\u3002\u53ef\u4ee5\u5b9e\u73b0\u4ee5\u4e0b\u76ee\u7684\uff1a<br \/>\n1. \u5728\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u8bbf\u95ee\u540e\u7aef\u8d44\u6e90\u4e4b\u524d\uff0c\u62e6\u622a\u8fd9\u4e9b\u8bf7\u6c42\u3002<br \/>\n2. \u5728\u670d\u52a1\u5668\u7684\u54cd\u5e94\u53d1\u9001\u56de\u5ba2\u6237\u7aef\u4e4b\u524d\uff0c\u5904\u7406\u8fd9\u4e9b\u54cd\u5e94\u3002<\/p>\n<h3>\u4e8c\u3001\u5de5\u4f5c\u539f\u7406<\/h3>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/12\/20161202104427.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1792\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/12\/20161202104427.png\" alt=\"20161202104427\" width=\"725\" height=\"309\" \/><\/a><\/p>\n<h3>\u4e09\u3001\u8fc7\u8651\u5668\u914d\u7f6e\u4e0e\u751f\u547d\u5468\u671f<\/h3>\n<p>[java]\u5b9e\u4f8b\u5316\u00a0\u00a0   web.xml<br \/>\n\u521d\u59cb\u5316\u00a0\u00a0\u00a0  init()<br \/>\n\u8fc7\u8651\u00a0\u00a0\u00a0\u00a0\u00a0  doFilter()<br \/>\n\u9500\u6bc1\u00a0\u00a0\u00a0\u00a0\u00a0  destroy()[\/java]<\/p>\n<p>web.xml\u914d\u7f6e\u5404\u8282\u70b9\u8bf4\u660e<\/p>\n<p>[java]&lt;filter&gt;              \u6307\u5b9a\u4e00\u4e2a\u8fc7\u6ee4\u5668\u3002<br \/>\n &lt;filter-name&gt;         \u7528\u4e8e\u4e3a\u8fc7\u6ee4\u5668\u6307\u5b9a\u4e00\u4e2a\u540d\u5b57\uff0c\u8be5\u5143\u7d20\u7684\u5185\u5bb9\u4e0d\u80fd\u4e3a\u7a7a\u3002<br \/>\n    &lt;filter-class&gt;        \u5143\u7d20\u7528\u4e8e\u6307\u5b9a\u8fc7\u6ee4\u5668\u7684\u5b8c\u6574\u7684\u9650\u5b9a\u7c7b\u540d\u3002<br \/>\n    &lt;init-param&gt;          \u5143\u7d20\u7528\u4e8e\u4e3a\u8fc7\u6ee4\u5668\u6307\u5b9a\u521d\u59cb\u5316\u53c2\u6570\uff0c\u5b83\u7684\u5b50\u5143\u7d20&lt;param-name&gt;\u6307\u5b9a\u53c2\u6570\u7684\u540d\u5b57\uff0c&lt;param-value&gt;\u6307\u5b9a\u53c2\u6570\u7684\u503c\u3002\u5728\u8fc7\u6ee4\u5668\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528FilterConfig\u63a5\u53e3\u5bf9\u8c61\u6765\u8bbf\u95ee\u521d\u59cb\u5316\u53c2\u6570\u3002<br \/>\n &lt;filter-mapping&gt;      \u5143\u7d20\u7528\u4e8e\u8bbe\u7f6e\u4e00\u4e2a Filter \u6240\u8d1f\u8d23\u62e6\u622a\u7684\u8d44\u6e90\u3002\u4e00\u4e2aFilter\u62e6\u622a\u7684\u8d44\u6e90\u53ef\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u6765\u6307\u5b9a\uff1aServlet \u540d\u79f0\u548c\u8d44\u6e90\u8bbf\u95ee\u7684\u8bf7\u6c42\u8def\u5f84<br \/>\n    &lt;filter-name&gt;         \u5b50\u5143\u7d20\u7528\u4e8e\u8bbe\u7f6efilter\u7684\u6ce8\u518c\u540d\u79f0\u3002\u8be5\u503c\u5fc5\u987b\u662f\u5728&lt;filter&gt;\u5143\u7d20\u4e2d\u58f0\u660e\u8fc7\u7684\u8fc7\u6ee4\u5668\u7684\u540d\u5b57<br \/>\n    &lt;url-pattern&gt;         \u8bbe\u7f6e filter \u6240\u62e6\u622a\u7684\u8bf7\u6c42\u8def\u5f84(\u8fc7\u6ee4\u5668\u5173\u8054\u7684URL\u6837\u5f0f)<br \/>\n &lt;servlet-name&gt;        \u6307\u5b9a\u8fc7\u6ee4\u5668\u6240\u62e6\u622a\u7684Servlet\u540d\u79f0\u3002[\/java]<\/p>\n<p>\u4f8b\u5b50:<\/p>\n<p>[java]public class LoginFilter implements Filter {<br \/>\n\tpublic void destroy() {<\/p>\n<p>\t}<br \/>\n\tpublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {<br \/>\n\t\t\/\/ place your code here<\/p>\n<p>\t\t\/\/ pass the request along the filter chain<br \/>\n\t\tchain.doFilter(request, response);<br \/>\n\t}<br \/>\n\tpublic void init(FilterConfig fConfig) throws ServletException {<\/p>\n<p>\t}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>web.xml\u914d\u7f6e<\/p>\n<p>[java]&lt;filter&gt;<br \/>\n\u00a0\u00a0 &lt;filter-name&gt;LoginFilter &lt;\/filter-name&gt;<br \/>\n\u00a0\u00a0 &lt;filter-class&gt;com.ittx.test.LoginFilter &lt;\/filter-class&gt;<br \/>\n\u00a0\u00a0 &lt;init-param&gt;<br \/>\n\u00a0\u00a0\u00a0 &lt;param-name&gt;test-param&lt;\/param-name&gt;<br \/>\n\u00a0\u00a0\u00a0 &lt;param-value&gt;Initialization Paramter&lt;\/param-value&gt;<br \/>\n\u00a0\u00a0 &lt;\/init-param&gt;<br \/>\n&lt;\/filter&gt;<\/p>\n<p>&lt;filter&gt;<br \/>\n\u00a0\u00a0 &lt;filter-name&gt;AuthenFilter&lt;\/filter-name&gt;<br \/>\n\u00a0\u00a0 &lt;filter-class&gt;com.ittx.test.AuthenFilter&lt;\/filter-class&gt;<br \/>\n\u00a0\u00a0 &lt;init-param&gt;<br \/>\n\u00a0\u00a0\u00a0 &lt;param-name&gt;test-param&lt;\/param-name&gt;<br \/>\n\u00a0\u00a0\u00a0 &lt;param-value&gt;Initialization Paramter&lt;\/param-value&gt;<br \/>\n\u00a0\u00a0 &lt;\/init-param&gt;<br \/>\n&lt;\/filter&gt;<\/p>\n<p>&lt;filter-mapping&gt;<br \/>\n\u00a0\u00a0 &lt;filter-name&gt;LoginFilter &lt;\/filter-name&gt;<br \/>\n\u00a0\u00a0 &lt;url-pattern&gt;\/*&lt;\/url-pattern&gt;<br \/>\n&lt;\/filter-mapping&gt;<\/p>\n<p>&lt;filter-mapping&gt;<br \/>\n\u00a0\u00a0 &lt;filter-name&gt;AuthenFilter&lt;\/filter-name&gt;<br \/>\n\u00a0\u00a0 &lt;url-pattern&gt;\/*&lt;\/url-pattern&gt;<br \/>\n&lt;\/filter-mapping&gt;[\/java]<\/p>\n<h3>\u56db\u3001\u8fc7\u8651\u94fe<\/h3>\n<p>\u4e00\u4e2aweb\u8bf7\u6c42\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u8fc7\u8651\u5668\uff0c\u5b83\u4eec\u6309\u5148\u540e\u987a\u5e8f\u7ec4\u88c5\u5728\u4e00\u8d77\u6210\u4e3a\u4e00\u6761\u8fc7\u8651\u94fe.<br \/>\n<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/12\/20161202105130.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1796\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/12\/20161202105130.png\" alt=\"20161202105130\" width=\"709\" height=\"254\" \/><\/a><\/p>\n<h3>\u4e94\u3001\u8fc7\u8651\u5668\u5206\u7c7b<\/h3>\n<p>REQUEST\uff1a\u5f53\u7528\u6237\u76f4\u63a5\u8bbf\u95ee\u9875\u9762\u65f6\uff0cWeb\u5bb9\u5668\u5c06\u4f1a\u8c03\u7528\u8fc7\u6ee4\u5668\u3002\u5982\u679c\u76ee\u6807\u8d44\u6e90\u662f\u901a\u8fc7RequestDispatcher\u7684include()\u6216forward()\u65b9\u6cd5\u8bbf\u95ee\u65f6\uff0c\u90a3\u4e48\u8be5\u8fc7\u6ee4\u5668\u5c31\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<p>INCLUDE\uff1a\u5982\u679c\u76ee\u6807\u8d44\u6e90\u662f\u901a\u8fc7RequestDispatcher\u7684include()\u65b9\u6cd5\u8bbf\u95ee\u65f6\uff0c\u90a3\u4e48\u8be5\u8fc7\u6ee4\u5668\u5c06\u88ab\u8c03\u7528\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u8be5\u8fc7\u6ee4\u5668\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<p>FORWARD\uff1a\u5982\u679c\u76ee\u6807\u8d44\u6e90\u662f\u901a\u8fc7RequestDispatcher\u7684forward()\u65b9\u6cd5\u8bbf\u95ee\u65f6\uff0c\u90a3\u4e48\u8be5\u8fc7\u6ee4\u5668\u5c06\u88ab\u8c03\u7528\uff0c\u9664\u6b64\u4e4b\u5916\uff0c\u8be5\u8fc7\u6ee4\u5668\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<p>ERROR\uff1a\u5982\u679c\u76ee\u6807\u8d44\u6e90\u662f\u901a\u8fc7\u58f0\u660e\u5f0f\u5f02\u5e38\u5904\u7406\u673a\u5236\u8c03\u7528\u65f6\uff0c\u90a3\u4e48\u8be5\u8fc7\u6ee4\u5668\u5c06\u88ab\u8c03\u7528\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u8fc7\u6ee4\u5668\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<p>\u5728web.xml\u4e2d\u914d\u7f6e<br \/>\n\u6307\u5b9a\u8fc7\u6ee4\u5668\u6240\u62e6\u622a\u7684\u8d44\u6e90\u88ab Servlet \u5bb9\u5668\u8c03\u7528\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u662fREQUEST,INCLUDE,FORWARD\u548cERROR\u4e4b\u4e00\uff0c\u9ed8\u8ba4REQUEST\u3002\u7528\u6237\u53ef\u4ee5\u8bbe\u7f6e\u591a\u4e2a\u5b50\u5143\u7d20\u7528\u6765\u6307\u5b9a Filter \u5bf9\u8d44\u6e90\u7684\u591a\u79cd\u8c03\u7528\u65b9\u5f0f\u8fdb\u884c\u62e6\u622a\u3002<\/p>\n<p>web\u76d1\u542c\u5668\u53c2\u89c1:<a title=\"Permalink to \u5982\u4f55\u4f7f\u7528\u76d1\u542c\u5668Servlet\" href=\"https:\/\/ai.zhousir.top\/?p=630\" rel=\"bookmark\">\u5982\u4f55\u4f7f\u7528\u76d1\u542c\u5668Servlet<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u8fc7\u8651\u5668\u5b9a\u4e49 \u8fc7\u8651\u5668\u662f\u4e00\u4e2a\u670d\u52a1\u7aef\u5b9e\u73b0javax.servlet.Filter\u63a5\u53e3\u7684java\u7c7b\uff0c\u5b83\u53ef\u4ee5\u622a\u53d6\u7528\u6237 [&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\/1791"}],"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=1791"}],"version-history":[{"count":16,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1791\/revisions"}],"predecessor-version":[{"id":1809,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1791\/revisions\/1809"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}