{"id":218965,"date":"2021-06-03T09:52:55","date_gmt":"2021-06-03T01:52:55","guid":{"rendered":"https:\/\/gulass.cn\/?p=218965"},"modified":"2021-05-23T10:54:34","modified_gmt":"2021-05-23T02:54:34","slug":"overview-of-the","status":"publish","type":"post","link":"https:\/\/gulass.cn\/overview-of-the.html","title":{"rendered":"Filter\u3001FilterChain\u3001FilterConfig\u539f\u7406\u53ca\u7528\u6cd5\u6982\u8ff0"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nFilter \u7a0b\u5e8f\u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86\u7279\u6b8a\u63a5\u53e3\u7684 Java \u7c7b\uff0c\u4e0e Servlet \u7c7b\u4f3c\uff0c\u4e5f\u662f\u7531 Servlet \u5bb9\u5668\u8fdb\u884c\u8c03\u7528\u548c\u6267\u884c\u7684\u3002FilterChain\u63a5\u53e3\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a Filter \u94fe\u7684\u5bf9\u8c61\u5e94\u8be5\u5bf9\u5916\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u63a5\u53e3\u53ea\u5b9a\u4e49\u4e86\u4e00\u4e2a doFilter \u65b9\u6cd5\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\u3001Filter \u7684\u57fa\u672c\u5de5\u4f5c\u539f\u7406<\/strong><\/div>\n
    \n
  1. Filter \u7a0b\u5e8f\u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86\u7279\u6b8a\u63a5\u53e3\u7684 Java \u7c7b\uff0c\u4e0e Servlet \u7c7b\u4f3c\uff0c\u4e5f\u662f\u7531 Servlet \u5bb9\u5668\u8fdb\u884c\u8c03\u7528\u548c\u6267\u884c\u7684\u3002<\/li>\n
  2. \u5f53\u5728 web.xml \u6ce8\u518c\u4e86\u4e00\u4e2a Filter \u6765\u5bf9\u67d0\u4e2a Servlet \u7a0b\u5e8f\u8fdb\u884c\u62e6\u622a\u5904\u7406\u65f6\uff0c\u5b83\u53ef\u4ee5\u51b3\u5b9a\u662f\u5426\u5c06\u8bf7\u6c42\u7ee7\u7eed\u4f20\u9012\u7ed9 Servlet \u7a0b\u5e8f\uff0c\u4ee5\u53ca\u5bf9\u8bf7\u6c42\u548c\u54cd\u5e94\u6d88\u606f\u662f\u5426\u8fdb\u884c\u4fee\u6539\u3002<\/li>\n
  3. \u5f53 Servlet \u5bb9\u5668\u5f00\u59cb\u8c03\u7528\u67d0\u4e2a Servlet \u7a0b\u5e8f\u65f6\uff0c\u5982\u679c\u53d1\u73b0\u5df2\u7ecf\u6ce8\u518c\u4e86\u4e00\u4e2a Filter \u7a0b\u5e8f\u6765\u5bf9\u8be5 Servlet \u8fdb\u884c\u62e6\u622a\uff0c\u90a3\u4e48\u5bb9\u5668\u4e0d\u518d\u76f4\u63a5\u8c03\u7528 Servlet \u7684 service \u65b9\u6cd5\uff0c\u800c\u662f\u8c03\u7528 Filter \u7684 doFilter \u65b9\u6cd5\uff0c\u518d\u7531 doFilter \u65b9\u6cd5\u51b3\u5b9a\u662f\u5426\u53bb\u6fc0\u6d3b service \u65b9\u6cd5\u3002<\/li>\n
  4. \u4f46\u5728 Filter.doFilter \u65b9\u6cd5\u4e2d\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528 Servlet \u7684 service \u65b9\u6cd5\uff0c\u800c\u662f\u8c03\u7528 FilterChain.doFilter \u65b9\u6cd5\u6765\u6fc0\u6d3b\u76ee\u6807 Servlet \u7684 service \u65b9\u6cd5\uff0cFilterChain \u5bf9\u8c61\u65f6\u901a\u8fc7 Filter.doFilter \u65b9\u6cd5\u7684\u53c2\u6570\u4f20\u9012\u8fdb\u6765\u7684\u3002<\/li>\n
  5. \u53ea\u8981\u5728 Filter.doFilter \u65b9\u6cd5\u4e2d\u8c03\u7528 FilterChain.doFilter \u65b9\u6cd5\u7684\u8bed\u53e5\u524d\u540e\u589e\u52a0\u67d0\u4e9b\u7a0b\u5e8f\u4ee3\u7801\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728 Servlet \u8fdb\u884c\u54cd\u5e94\u524d\u540e\u5b9e\u73b0\u67d0\u4e9b\u7279\u6b8a\u529f\u80fd\u3002<\/li>\n
  6. \u5982\u679c\u5728 Filter.doFilter \u65b9\u6cd5\u4e2d\u6ca1\u6709\u8c03\u7528 FilterChain.doFilter \u65b9\u6cd5\uff0c\u5219\u76ee\u6807 Servlet \u7684 service \u65b9\u6cd5\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u8fd9\u6837\u901a\u8fc7 Filter \u5c31\u53ef\u4ee5\u963b\u6b62\u67d0\u4e9b\u975e\u6cd5\u7684\u8bbf\u95ee\u8bf7\u6c42\u3002<\/li>\n<\/ol>\n
    \u4e8c\u3001Filter \u94fe<\/strong><\/div>\n
      \n
    1. \u5728\u4e00\u4e2a Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u53ef\u4ee5\u6ce8\u518c\u591a\u4e2a Filter \u7a0b\u5e8f\uff0c\u6bcf\u4e2a Filter \u7a0b\u5e8f\u90fd\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u6216\u4e00\u7ec4 Servlet \u7a0b\u5e8f\u8fdb\u884c\u62e6\u622a\u3002\u5982\u679c\u6709\u591a\u4e2a Filter \u7a0b\u5e8f\u90fd\u53ef\u4ee5\u5bf9\u67d0\u4e2a Servlet \u7a0b\u5e8f\u7684\u8bbf\u95ee\u8fc7\u7a0b\u8fdb\u884c\u62e6\u622a\uff0c\u5f53\u9488\u5bf9\u8be5 Servlet \u7684\u8bbf\u95ee\u8bf7\u6c42\u5230\u8fbe\u65f6\uff0cWeb \u5bb9\u5668\u5c06\u628a\u8fd9\u591a\u4e2a Filter \u7a0b\u5e8f\u7ec4\u5408\u6210\u4e00\u4e2a Filter \u94fe\uff08\u4e5f\u53eb\u8fc7\u6ee4\u5668\u94fe\uff09\u3002<\/li>\n
    2. Filter \u94fe\u4e2d\u7684\u5404\u4e2a Filter \u7684\u62e6\u622a\u987a\u5e8f\u4e0e\u5b83\u4eec\u5728 web.xml \u6587\u4ef6\u4e2d\u7684\u6620\u5c04\u987a\u5e8f\u4e00\u81f4\uff0c\u4e0a\u4e00\u4e2a Filter.doFilter \u65b9\u6cd5\u4e2d\u8c03\u7528 FilterChain.doFilter \u65b9\u6cd5\u5c06\u6fc0\u6d3b\u4e0b\u4e00\u4e2a Filter\u7684doFilter \u65b9\u6cd5\uff0c\u6700\u540e\u4e00\u4e2a Filter.doFilter \u65b9\u6cd5\u4e2d\u8c03\u7528\u7684 FilterChain.doFilter \u65b9\u6cd5\u5c06\u6fc0\u6d3b\u76ee\u6807 Servlet\u7684service \u65b9\u6cd5\u3002<\/li>\n
    3. \u53ea\u8981 Filter \u94fe\u4e2d\u4efb\u610f\u4e00\u4e2a Filter \u6ca1\u6709\u8c03\u7528 FilterChain.doFilter \u65b9\u6cd5\uff0c\u5219\u76ee\u6807 Servlet \u7684 service \u65b9\u6cd5\u90fd\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/li>\n<\/ol>\n
      \u4e09\u3001Filter \u63a5\u53e3<\/strong><\/div>\n

      \u4e00\u4e2a Filter \u7a0b\u5e8f\u5c31\u662f\u4e00\u4e2a Java \u7c7b\uff0c\u8fd9\u4e2a\u7c7b\u5fc5\u987b\u5b9e\u73b0 Filter \u63a5\u53e3\u3002javax.servlet.Filter \u63a5\u53e3\u4e2d\u5b9a\u4e49\u4e86\u4e09\u4e2a\u65b9\u6cd5\uff1ainit\u3001doFilter\u3001destory\u3002<\/p>\n

      1\u3001init \u65b9\u6cd5<\/p>\n

      \uff081\uff09\u3001\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\uff0cWeb \u670d\u52a1\u5668\uff08Web \u5bb9\u5668\uff09\u5c06\u6839\u636e\u5176 web.xml \u6587\u4ef6\u7684\u914d\u7f6e\u4fe1\u606f\u6765\u521b\u5efa\u6bcf\u4e2a\u6ce8\u518c\u7684 Filter \u7684\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\u3002
      \n\uff082\uff09\u3001Web \u5bb9\u5668\u521b\u5efa Filter \u7684\u5b9e\u4f8b\u5bf9\u8c61\u540e\uff0c\u5c06\u7acb\u5373\u8c03\u7528\u8be5 Filter \u5bf9\u8c61\u7684 init \u65b9\u6cd5\u3002init \u65b9\u6cd5\u5728 Filter \u751f\u547d\u5468\u671f\u4e2d\u4ec5\u88ab\u6267\u884c\u4e00\u6b21\uff0cWeb \u5bb9\u5668\u5728\u8c03\u7528 init \u65b9\u6cd5\u65f6\uff0c\u4f1a\u4f20\u9012\u4e00\u4e2a\u5305\u542b Filter \u7684\u914d\u7f6e\u548c\u8fd0\u884c\u73af\u5883\u4fe1\u606f\u7684 FilterConfig \u5bf9\u8c61\u3002<\/p>\n

      public voic init(FilterConfig filterConfig) throws ServletException<\/pre>\n

      \uff083\uff09\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5728 init \u65b9\u6cd5\u4e2d\u5b8c\u6210\u4e0e\u6784\u9020\u65b9\u6cd5\u7c7b\u4f3c\u7684\u521d\u59cb\u5316\u529f\u80fd\uff0c\u8981\u6ce8\u610f\u7684\u662f\uff1a\u5982\u679c\u521d\u59cb\u5316\u4ee3\u7801\u8981\u4f7f\u7528\u5230 FilterConfig \u5bf9\u8c61\uff0c\u8fd9\u4e9b\u4ee3\u7801\u53ea\u80fd\u5728 init \u65b9\u6cd5\u4e2d\u7f16\u5199\uff0c\u800c\u4e0d\u80fd\u5728\u6784\u9020\u65b9\u6cd5\u4e2d\u7f16\u5199\uff08\u5c1a\u672a\u8c03\u7528 init \u65b9\u6cd5\uff0c\u5373\u5e76\u6ca1\u6709\u521b\u5efa FilterConfig \u5bf9\u8c61\uff0c\u8981\u4f7f\u7528\u5b83\u5219\u5fc5\u7136\u51fa\u9519\uff09\u3002
      \n2\u3001doFilter \u65b9\u6cd5
      \n\u5f53\u4e00\u4e2a Filter \u5bf9\u8c61\u80fd\u591f\u62e6\u622a\u8bbf\u95ee\u8bf7\u6c42\u65f6\uff0cServlet \u5bb9\u5668\u5c06\u8c03\u7528 Filter \u5bf9\u8c61\u7684 doFilter \u65b9\u6cd5\u3002<\/p>\n

      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException.ServletException<\/pre>\n

      \u5176\u4e2d\uff0c\u53c2\u6570 request \u548c response \u4e3a Web \u5bb9\u5668\u6216 Filter \u94fe\u4e2d\u4e0a\u4e00\u4e2a Filter \u4f20\u9012\u8fc7\u6765\u7684\u8bf7\u6c42\u548c\u54cd\u5e94\u5bf9\u8c61\uff1b\u53c2\u6570 chain \u4e3a\u4ee3\u8868\u5f53\u524d Filter \u94fe\u7684\u5bf9\u8c61\u3002<\/p>\n

      3\u3001destroy \u65b9\u6cd5
      \n\u8be5\u65b9\u6cd5\u5728 Web \u5bb9\u5668\u5378\u8f7d Filter \u5bf9\u8c61\u4e4b\u524d\u88ab\u8c03\u7528\uff0c\u4e5f\u4ec5\u6267\u884c\u4e00\u6b21\u3002\u53ef\u4ee5\u5b8c\u6210\u4e0e init \u65b9\u6cd5\u76f8\u53cd\u7684\u529f\u80fd\uff0c\u91ca\u653e\u88ab\u8be5 Filter \u5bf9\u8c61\u6253\u5f00\u7684\u8d44\u6e90\uff0c\u4f8b\u5982\uff1a\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u548c IO \u6d41\u3002<\/p>\n

      \u56db\u3001FilterChain \u63a5\u53e3<\/strong><\/div>\n

      \u8be5\u63a5\u53e3\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a Filter \u94fe\u7684\u5bf9\u8c61\u5e94\u8be5\u5bf9\u5916\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u63a5\u53e3\u53ea\u5b9a\u4e49\u4e86\u4e00\u4e2a doFilter \u65b9\u6cd5\u3002<\/p>\n

      public void doFilter(ServletRequest request, ServletResponse response) throws java.io.IOException.ServletException<\/pre>\n

      FilterChain \u63a5\u53e3\u7684 doFilter \u65b9\u6cd5\u7528\u4e8e\u901a\u77e5 Web \u5bb9\u5668\u628a\u8bf7\u6c42\u4ea4\u7ed9 Filter \u94fe\u4e2d\u7684\u4e0b\u4e00\u4e2a Filter \u53bb\u5904\u7406\uff0c\u5982\u679c\u5f53\u524d\u8c03\u7528\u6b64\u65b9\u6cd5\u7684 Filter \u5bf9\u8c61\u662fFilter \u94fe\u4e2d\u7684\u6700\u540e\u4e00\u4e2a Filter\uff0c\u90a3\u4e48\u5c06\u628a\u8bf7\u6c42\u4ea4\u7ed9\u76ee\u6807 Servlet \u7a0b\u5e8f\u53bb\u5904\u7406\u3002<\/p>\n

      \u4e94\u3001FilterConfig \u63a5\u53e3<\/strong><\/div>\n

      1\u3001\u4e0e\u666e\u901a\u7684 Servlet \u7a0b\u5e8f\u4e00\u6837\uff0cFilter \u7a0b\u5e8f\u4e5f\u5f88\u53ef\u80fd\u9700\u8981\u8bbf\u95ee Servlet \u5bb9\u5668\u3002Servlet \u89c4\u8303\u5c06\u4ee3\u8868 ServletContext \u5bf9\u8c61\u548c Filter \u7684\u914d\u7f6e\u53c2\u6570\u4fe1\u606f\u90fd\u5c01\u88c5\u5230\u4e00\u4e2a\u79f0\u4e3a FilterConfig \u7684\u5bf9\u8c61\u4e2d\u3002
      \n2\u3001FilterConfig \u63a5\u53e3\u5219\u7528\u4e8e\u5b9a\u4e49 FilterConfig \u5bf9\u8c61\u5e94\u8be5\u5bf9\u5916\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u5728 Filter \u7a0b\u5e8f\u4e2d\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u83b7\u53d6 ServletContext \u5bf9\u8c61\uff0c\u4ee5\u53ca\u83b7\u53d6\u5728 web.xml \u6587\u4ef6\u4e2d\u4e3a Filter \u8bbe\u7f6e\u7684\u53cb\u597d\u540d\u79f0\u548c\u521d\u59cb\u5316\u53c2\u6570\u3002
      \n3\u3001FilterConfig\u63a5\u53e3\u5b9a\u4e49\u7684\u5404\u4e2a\u65b9\u6cd5\uff1a<\/p>\n

        \n
      • \n

        getFilterName \u65b9\u6cd5\uff0c\u8fd4\u56de <filter-name> \u5143\u7d20\u7684\u8bbe\u7f6e\u503c\u3002<\/p>\n<\/li>\n

      • \n

        getServletContext \u65b9\u6cd5\uff0c\u8fd4\u56de FilterConfig \u5bf9\u8c61\u4e2d\u6240\u5305\u88c5\u7684 ServletContext \u5bf9\u8c61\u7684\u5f15\u7528\u3002<\/p>\n<\/li>\n

      • \n

        getInitParameter \u65b9\u6cd5\uff0c\u7528\u4e8e\u8fd4\u56de\u5728 web.xml \u6587\u4ef6\u4e2d\u4e3a Filter \u6240\u8bbe\u7f6e\u7684\u67d0\u4e2a\u540d\u79f0\u7684\u521d\u59cb\u5316\u7684\u53c2\u6570\u503c\u3002<\/p>\n<\/li>\n

      • \n

        getInitParameterNames \u65b9\u6cd5\uff0c\u8fd4\u56de\u4e00\u4e2a Enumeration \u96c6\u5408\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<\/ul>\n

        \u516d\u3001Filter \u7684\u6ce8\u518c\u4e0e\u6620\u5c04<\/strong><\/div>\n
        1\u3001\u6ce8\u518c Filter<\/strong><\/span><\/div>\n

        \u4e00\u4e2a <filter> \u5143\u7d20\u7528\u4e8e\u6ce8\u518c\u4e00\u4e2a Filter\u3002\u5176\u4e2d\uff0c<filter-name> \u5143\u7d20\u662f\u5fc5\u9700\u7684\uff0c<filter-class> \u5143\u7d20\u4e5f\u662f\u5fc5\u9700\u7684\uff0c<init-param> \u5143\u7d20\u662f\u53ef\u9009\u7684\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a < init-param> \u5143\u7d20\u3002<\/p>\n

        <filter><\/span>\r\n    <\/span><filter-name><\/span>FirstFilter<\/span><\/filter-name><\/span>\r\n    <\/span><filter-class><\/span>FirstFilter<\/span><\/filter-class><\/span>\r\n    <\/span><init-param><\/span>\r\n        <\/span><param-name><\/span>encoding<\/span><\/param-name><\/span>\r\n        <\/span><param-value><\/span>GB2312<\/span><\/param-value><\/span>\r\n    <\/span><\/init-param><\/span>\r\n<\/span><\/filter><\/span><\/pre>\n
        2\u3001\u6620\u5c04 Filter<\/strong><\/span><\/div>\n

        <filter-mapping> \u5143\u7d20\u7528\u4e8e\u8bbe\u7f6e\u4e00\u4e2a Filter \u6240\u8d1f\u8d23\u62e6\u622a\u7684\u8d44\u6e90\u3002\u4e00\u4e2a Filter \u62e6\u622a\u7684\u8d44\u6e90\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u6765\u6307\u5b9a\uff1a\u8d44\u6e90\u7684\u8bbf\u95ee\u8bf7\u6c42\u8def\u5f84\u548c Servlet \u540d\u79f0\u3002<\/p>\n

        \u7b2c\u4e00\u79cd\uff1a\u6307\u5b9a\u8d44\u6e90\u7684\u8bbf\u95ee\u8def\u5f84<\/strong><\/p>\n

        <filter-mapping><\/span>\r\n    <\/span><filter-name><\/span>FirstFilter<\/span><\/filter-name><\/span>\r\n    <\/span><url-pattern><\/span>\/*<\/span><\/url-pattern><\/span>\r\n<\/span><\/filter-mapping><\/span><\/pre>\n

        <url-pattern> \u5143\u7d20\u4e2d\u7684\u8bbf\u95ee\u8def\u5f84\u7684\u8bbe\u7f6e\u65b9\u5f0f\u9075\u5faa Servlet \u7684 URL \u6620\u5c04\u89c4\u8303\u3002<\/p>\n

          \n
        • \n\/*<\/span>\uff1a\u8868\u793a\u62e6\u622a\u6240\u6709\u7684\u8bbf\u95ee\u8bf7\u6c42\u3002\n<\/li>\n
        • \n\/filter\/*<\/span>\uff1a\u8868\u793a\u62e6\u622a filter \u76ee\u5f55\u4e0b\u7684\u6240\u6709\u8bbf\u95ee\u8bf7\u6c42\uff0c\u5982\uff1ahttp:\/\/localhost:8888\/testFilter_001\/filter\/xxxxxx
          \n\u3002<\/li>\n
        • \n\/test.html<\/span>\uff1a\u8868\u793a\u62e6\u622a\u6839\u76ee\u5f55\u4e0b\u4ee5 test.html \u4e3a\u8d44\u6e90\u540d\u7684\u8bbf\u95ee\u8bf7\u6c42\uff0c\u8bbf\u95ee\u94fe\u63a5\u53ea\u4f1a\u662f\uff1ahttp:\/\/localhost:8888\/test.html\u3002<\/li>\n<\/ul>\n

          \u7b2c\u4e8c\u79cd\uff1a\u6307\u5b9a Servlet \u7684\u540d\u79f0<\/strong><\/p>\n

          <filter-mapping><\/span>\r\n    <\/span><filter-name><\/span>FirstFilter<\/span><\/filter-name><\/span>\r\n    <\/span><servlet-name><\/span>default><\/span><\/servlet-name><\/span>\r\n    <\/span><dispatcher><\/span>INCLUDE<\/span><\/dispatcher><\/span>\r\n    <\/span><dispatcher><\/span>REQUEST<\/span><\/dispatcher><\/span>\r\n<\/span><\/filter-mapping><\/span><\/pre>\n

          \uff081\uff09\u3001<servlet-name> \u5143\u7d20\u4e0e <url-pattern> \u5143\u7d20\u662f\u4e8c\u9009\u4e00\u7684\u5173\u7cfb\uff0c\u5176\u503c\u662f\u67d0\u4e2a Servlet \u5728 web.xml \u6587\u4ef6\u4e2d\u7684\u6ce8\u518c\u540d\u79f0\u3002<\/p>\n

          \uff082\uff09\u3001<dispatcher> \u5143\u7d20\u7684\u8bbe\u7f6e\u503c\u6709 4 \u79cd\uff1aREQUEST\u3001INCLUDE\u3001FORWARD\u3001ERROR\uff0c\u5206\u522b\u5bf9\u5e94 Servlet \u5bb9\u5668\u8c03\u7528\u8d44\u6e90\u7684 4 \u79cd\u65b9\u5f0f\uff1a<\/p>\n

          \u901a\u8fc7\u6b63\u5e38\u7684\u8bbf\u95ee\u8bf7\u6c42\u8c03\u7528\uff1b<\/p>\n

          \u901a\u8fc7 RequestDispatcher.include \u65b9\u6cd5\u8c03\u7528\uff1b<\/p>\n

          \u901a\u8fc7 RequestDispatcher.forward \u65b9\u6cd5\u8c03\u7528\uff1b<\/p>\n

          \u4f5c\u4e3a\u9519\u8bef\u54cd\u5e94\u8d44\u6e90\u8c03\u7528\u3002<\/p>\n

          \u5982\u679c\u6ca1\u6709\u8bbe\u7f6e <dispatcher> \u5b50\u5143\u7d20\uff0c\u5219\u7b49\u6548\u4e8e REQUEST \u7684\u60c5\u51b5\u3002\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u591a\u4e2a <dispatcher> \u5b50\u5143\u7d20\uff0c\u7528\u4e8e\u6307\u5b9a Filter \u5bf9\u8d44\u6e90\u7684\u591a\u79cd\u8c03\u7528\u65b9\u5f0f\u90fd\u8fdb\u884c\u62e6\u622a\u3002<\/p>\n

          \u4e03\u3001Filter \u7a0b\u5e8f\u793a\u4f8b<\/strong><\/div>\n

          FitstFilter.java<\/strong><\/p>\n

          import<\/span> <\/span>java<\/span>.<\/span>io<\/span>.<\/span>IOException<\/span>;\r\n<\/span>import<\/span> <\/span>java<\/span>.<\/span>io<\/span>.<\/span>PrintWriter<\/span>;\r\n<\/span>import<\/span> <\/span>java<\/span>.<\/span>util<\/span>.<\/span>Enumeration<\/span>;\r\n<\/span>import<\/span> <\/span>javax<\/span>.<\/span>servlet<\/span>.<\/span>Filter<\/span>;\r\n<\/span>import<\/span> <\/span>javax<\/span>.<\/span>servlet<\/span>.<\/span>FilterChain<\/span>;\r\n<\/span>import<\/span> <\/span>javax<\/span>.<\/span>servlet<\/span>.<\/span>FilterConfig<\/span>;\r\n<\/span>import<\/span> <\/span>javax<\/span>.<\/span>servlet<\/span>.<\/span>ServletException<\/span>;\r\n<\/span>import<\/span> <\/span>javax<\/span>.<\/span>servlet<\/span>.<\/span>ServletRequest<\/span>;\r\n<\/span>import<\/span> <\/span>javax<\/span>.<\/span>servlet<\/span>.<\/span>ServletResponse<\/span>;\r\n<\/span>import<\/span> <\/span>javax<\/span>.<\/span>servlet<\/span>.<\/span>http<\/span>.<\/span>HttpServletRequest<\/span>;\r\n \r\n<\/span>public<\/span> <\/span>class<\/span> <\/span>FirstFilter<\/span> <\/span>implements<\/span> <\/span>Filter<\/span> <\/span>{<\/span>\r\n <\/span>private<\/span> <\/span>FilterConfig<\/span> <\/span>filterConfig<\/span> = <\/span>null<\/span>;\r\n <\/span>String<\/span> <\/span>paramValue<\/span> = <\/span>null<\/span>;\r\n \r\n @<\/span>Override<\/span>\r\n <\/span>public<\/span> <\/span>void<\/span> <\/span>init<\/span>(<\/span>FilterConfig<\/span> <\/span>filterConfig<\/span>)<\/span> <\/span>throws<\/span> <\/span>ServletException<\/span> <\/span>{<\/span>\r\n <\/span>this<\/span>.<\/span>filterConfig<\/span> = <\/span>filterConfig<\/span>;\r\n <\/span>paramValue<\/span> = <\/span>filterConfig<\/span>.<\/span>getInitParameter<\/span>(<\/span>\"<\/span>encoding<\/span>\"<\/span>)<\/span>;\r\n <\/span>}<\/span>\r\n \r\n @<\/span>Override<\/span>\r\n <\/span>public<\/span> <\/span>void<\/span> <\/span>doFilter<\/span>(<\/span>ServletRequest<\/span> <\/span>request<\/span>, <\/span>ServletResponse<\/span> <\/span>response<\/span>,\r\n <\/span>FilterChain<\/span> <\/span>chain<\/span>)<\/span> <\/span>throws<\/span> <\/span>IOException<\/span>, <\/span>ServletException<\/span> <\/span>{<\/span>\r\n <\/span>System<\/span>.<\/span>out<\/span>.<\/span>println<\/span>(<\/span>\"<\/span>begin headers-------------------<\/span>\"<\/span>)<\/span>;\r\n <\/span>Enumeration<\/span><?> <\/span>headerNames<\/span> = <\/span>(<\/span>(<\/span>HttpServletRequest<\/span>)<\/span>request<\/span>)<\/span>.<\/span>getHeaderNames<\/span>(<\/span>)<\/span>;\r\n \r\n <\/span>while<\/span>(<\/span>headerNames<\/span>.<\/span>hasMoreElements<\/span>(<\/span>)<\/span>)<\/span> <\/span>{<\/span>\r\n <\/span>String<\/span> <\/span>headerName<\/span> = <\/span>(<\/span>String<\/span>)<\/span>headerNames<\/span>.<\/span>nextElement<\/span>(<\/span>)<\/span>;\r\n <\/span>System<\/span>.<\/span>out<\/span>.<\/span>println<\/span>(<\/span>headerName<\/span> + <\/span>\"<\/span>: <\/span>\"<\/span> + <\/span>(<\/span>(<\/span>HttpServletRequest<\/span>)<\/span>request<\/span>)<\/span>.<\/span>getHeader<\/span>(<\/span>headerName<\/span>)<\/span>)<\/span>;\r\n <\/span>}<\/span>\r\n <\/span>System<\/span>.<\/span>out<\/span>.<\/span>println<\/span>(<\/span>\"<\/span>end headers-------------------<\/span>\"<\/span>)<\/span>;\r\n \r\n <\/span>\/\/<\/span>\u5728\u8c03\u7528\u76ee\u6807\u524d\u5199\u5165\u54cd\u5e94\u5185\u5bb9<\/span><\/span>\r\n <\/span>response<\/span>.<\/span>setContentType<\/span>(<\/span>\"<\/span>text\/html; charset=gb2312<\/span>\"<\/span>)<\/span>;\r\n <\/span>PrintWriter<\/span> <\/span>out<\/span> = <\/span>response<\/span>.<\/span>getWriter<\/span>(<\/span>)<\/span>;\r\n <\/span>out<\/span>.<\/span>println<\/span>(<\/span>\"<\/span>IP\u5730\u5740\u4e3a\uff1a<\/span>\"<\/span> + <\/span>request<\/span>.<\/span>getRemoteHost<\/span>(<\/span>)<\/span> + <\/span>\"<\/span><br><\/span>\"<\/span>)<\/span>;\r\n \r\n <\/span>chain<\/span>.<\/span>doFilter<\/span>(<\/span>request<\/span>, <\/span>response<\/span>)<\/span>;\r\n \r\n <\/span>\/\/<\/span>\u5728\u76ee\u6807\u8fd4\u56de\u540e\u5199\u5165\u54cd\u5e94\u5185\u5bb9<\/span><\/span>\r\n <\/span>out<\/span>.<\/span>println<\/span>(<\/span>\"<\/span><br>\u540d\u79f0\u4e3aencoding\u7684\u521d\u59cb\u5316\u53c2\u6570\u7684\u503c\u4e3a\uff1a<\/span>\"<\/span> + <\/span>paramValue<\/span>)<\/span>;\r\n <\/span>out<\/span>.<\/span>println<\/span>(<\/span>\"<\/span><br>\u5f53\u524dWeb\u7a0b\u5e8f\u7684\u771f\u5b9e\u8def\u5f84\u4e3a\uff1a<\/span>\"<\/span> + <\/span>filterConfig<\/span>.<\/span>getServletContext<\/span>(<\/span>)<\/span>.<\/span>getRealPath<\/span>(<\/span>\"<\/span>\/<\/span>\"<\/span>)<\/span>)<\/span>;\r\n \r\n <\/span>\/\/<\/span>out.println(\"<br>\u4fee\u6539\u4e86test.html\u6587\u4ef6\uff01\");<\/span><\/span>\r\n <\/span>}<\/span>\r\n \r\n @<\/span>Override<\/span>\r\n <\/span>public<\/span> <\/span>void<\/span> <\/span>destroy<\/span>(<\/span>)<\/span> <\/span>{<\/span>\r\n <\/span>this<\/span>.<\/span>filterConfig<\/span> = <\/span>null<\/span>;\r\n <\/span>}<\/span>\r\n<\/span>}<\/span><\/div><\/pre>\n

          web.xml<\/strong><\/p>\n

          <<\/span>filter<\/span>><\/span>\r\n <\/span><<\/span>filter-name<\/span>><\/span>FirstFilter<\/span><\/<\/span>filter-name<\/span>><\/span>\r\n <\/span><<\/span>filter-class<\/span>><\/span>FirstFilter<\/span><\/<\/span>filter-class<\/span>><\/span>\r\n <\/span><<\/span>init-param<\/span>><\/span>\r\n <\/span><<\/span>param-name<\/span>><\/span>encoding<\/span><\/<\/span>param-name<\/span>><\/span>\r\n <\/span><<\/span>param-value<\/span>><\/span>GB2312<\/span><\/<\/span>param-value<\/span>><\/span>\r\n <\/span><\/<\/span>init-param<\/span>><\/span>\r\n<\/span><\/<\/span>filter<\/span>><\/span>\r\n \r\n<\/span><<\/span>filter-mapping<\/span>><\/span>\r\n <\/span><<\/span>filter-name<\/span>><\/span>FirstFilter<\/span><\/<\/span>filter-name<\/span>><\/span>\r\n <\/span><<\/span>url-pattern<\/span>><\/span>\/*<\/span><\/<\/span>url-pattern<\/span>><\/span>\r\n<\/span><\/<\/span>filter-mapping<\/span>><\/span><\/div><\/pre>\n

          test.html\uff08\u4f4d\u4e8eWebContent\u8def\u5f84\u7684filter\u76ee\u5f55\u4e2d\uff09<\/strong><\/p>\n
          <<\/span>html<\/span>><\/span>\r\n<\/span><<\/span>head<\/span>><\/span>\r\n<\/span><<\/span>meta<\/span> <\/span>charset<\/span>=<\/span>\"<\/span>UTF-8<\/span>\"<\/span>><\/span>\r\n<\/span><<\/span>title<\/span>><\/span>Insert title here<\/span><\/<\/span>title<\/span>><\/span>\r\n<\/span><\/<\/span>head<\/span>><\/span>\r\n<\/span><<\/span>body<\/span>><\/span>\r\n\u8fd9\u5c31\u662ftest.html\u9875\u9762\u7684\u539f\u59cb\u5185\u5bb9\uff01\r\n<\/span><\/<\/span>body<\/span>><\/span>\r\n<\/span><\/<\/span>html<\/span>><\/span><\/div><\/pre>\n

          \u8bbf\u95ee\uff1ahttp:\/\/localhost:8888\/testFilter_001\/filter\/test.html<\/p>\n","protected":false},"excerpt":{"rendered":"

          Filter \u7a0b\u5e8f\u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86\u7279\u6b8a\u63a5\u53e3\u7684 Java \u7c7b\uff0c\u4e0e Servlet \u7c7b\u4f3c\uff0c\u4e5f\u662f\u7531 Servlet \u5bb9\u5668 […]<\/p>\n","protected":false},"author":1481,"featured_media":218973,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-218965","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/218965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=218965"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/218965\/revisions"}],"predecessor-version":[{"id":219003,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/218965\/revisions\/219003"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/218973"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=218965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=218965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=218965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}