{"id":2022,"date":"2017-06-17T14:34:47","date_gmt":"2017-06-17T06:34:47","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=2022"},"modified":"2017-06-17T14:34:47","modified_gmt":"2017-06-17T06:34:47","slug":"springboot%e4%b8%ad%e4%bd%bf%e7%94%a8servlet%e4%b8%8e%e6%9b%b4%e6%94%b9dispatcherservlet%e9%bb%98%e8%ae%a4url","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=2022","title":{"rendered":"SpringBoot\u4e2d\u4f7f\u7528Servlet\u4e0e\u66f4\u6539DispatcherServlet\u9ed8\u8ba4Url"},"content":{"rendered":"<p>Springboot\u5f00\u53d1web\u5e94\u7528\u8fc7\u7a0b\u4e2d\u8981\u7528\u5230\u8fc7\u6ee4Filter\u548c\u76d1\u542c\u5668Listener,<br \/>\nSpringboot\u7684\u4e3bServlet\u4e3a DispatcherServlet\uff0c\u5176\u9ed8\u8ba4\u7684url-pattern\u4e3a\u201c\/\u201d\u3002\u4e5f\u8bb8\u6211\u4eec\u5728\u5e94\u7528\u4e2d\u8fd8\u9700\u8981\u5b9a\u4e49\u66f4\u591a\u7684Servlet\uff0c\u8be5\u5982\u4f55\u4f7f\u7528SpringBoot\u6765\u5b8c\u6210\u5462\uff1f<\/p>\n<p>\u5728spring boot\u4e2d\u6dfb\u52a0\u81ea\u5df1\u7684Servlet\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u4ee3\u7801\u6ce8\u518cServlet\u548c\u6ce8\u89e3\u81ea\u52a8\u6ce8\u518c\uff08Filter\u548cListener\u4e5f\u662f\u5982\u6b64\uff09\u3002<\/p>\n<h3>\n\u4e00\u3001\u4ee3\u7801\u6ce8\u518c\u901a\u8fc7ServletRegistrationBean\u3001 FilterRegistrationBean \u548c ServletListenerRegistrationBean \u83b7\u5f97\u63a7\u5236\u3002<\/h3>\n<p>\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 ServletContextInitializer \u63a5\u53e3\u76f4\u63a5\u6ce8\u518c\u3002<\/p>\n<h4>1.\u5b9e\u73b0MyServlet.java<\/h4>\n<p>public class MyServlet extends HttpServlet{<\/p>\n<p>[java]    @Override<br \/>\n    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {<br \/>\n        System.out.println(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;doGet()&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&quot;);<br \/>\n        doPost(req, resp);<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {<br \/>\n        System.out.println(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;doPost()&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&quot;);<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>}<\/p>\n<h4>2.SpringBoot\u542f\u52a8\u65f6\u4ee3\u7801\u6ce8\u5165Servlet<\/h4>\n<p>[java]@SpringBootApplication<br \/>\npublic class SpringBootSampleApplication {<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        SpringApplication.run(SpringBootSampleApplication.class, args);<br \/>\n    }<\/p>\n<p>   \/**<br \/>\n     * \u4fee\u6539DispatcherServlet\u9ed8\u8ba4\u914d\u7f6e<br \/>\n     *<br \/>\n     *\/<br \/>\n    @Bean<br \/>\n    public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {<br \/>\n        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);<br \/>\n        registration.getUrlMappings().clear();<br \/>\n        registration.addUrlMappings(&quot;*.do&quot;);<br \/>\n        registration.addUrlMappings(&quot;*.json&quot;);<br \/>\n        return registration;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u4f7f\u7528\u4ee3\u7801\u6ce8\u518cServlet\uff08\u4e0d\u9700\u8981@ServletComponentScan\u6ce8\u89e3\uff09<br \/>\n     *\/<br \/>\n    @Bean<br \/>\n    public ServletRegistrationBean servletRegistrationBean() {<br \/>\n        return new ServletRegistrationBean(new MyServlet(), &quot;\/xs\/*&quot;);\/\/ ServletName\u9ed8\u8ba4\u503c\u4e3a\u9996\u5b57\u6bcd\u5c0f\u5199\uff0c\u5373myServlet<br \/>\n    }<br \/>\n}[\/java]<\/p>\n<h3>\u4e8c\u3001\u5728 SpringBootApplication \u4e0a\u4f7f\u7528@ServletComponentScan \u6ce8\u89e3\u540e\uff0cServlet\u3001Filter\u3001Listener \u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7 @WebServlet\u3001@WebFilter\u3001@WebListener \u6ce8\u89e3\u81ea\u52a8\u6ce8\u518c\uff0c\u65e0\u9700\u5176\u4ed6\u4ee3\u7801\u3002<\/h3>\n<h4>1. \u8fc7\u6ee4\u5668LoginFilter.java<\/h4>\n<p>[java]@WebFilter(value=&quot;\/*&quot;)<br \/>\npublic class LoginFilter implements Filter {<\/p>\n<p>        public LoginFilter() {<br \/>\n    \t       System.out.println(&quot;\u8fc7\u6ee4\u5668\u521d\u59cb\u5316&quot;);<br \/>\n        }<\/p>\n<p>\tpublic void destroy() {<br \/>\n\t\t System.out.println(&quot;\u8fc7\u6ee4\u5668\u9500\u6bc1&quot;);<br \/>\n\t}<\/p>\n<p>\tpublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {<br \/>\n\t\t\/\/ TODO Auto-generated method stub<br \/>\n\t\t\/\/ place your code here<br \/>\n\t\t  System.out.println(&quot;\u6267\u884c\u8fc7\u6ee4\u64cd\u4f5c&gt;&gt;&gt;&gt;&gt;&quot;);<br \/>\n\t\t\/\/ pass the request along the filter chain<\/p>\n<p>\t\tchain.doFilter(request, response);<br \/>\n\t}<\/p>\n<p>\tpublic void init(FilterConfig fConfig) throws ServletException {<br \/>\n\t}<\/p>\n<p>}[\/java]<\/p>\n<h4>2.SpringBoot\u542f\u52a8\u7c7b\u6dfb\u52a0\u7ec4\u4ef6\u626b\u63cf\u6ce8\u89e3@ServletComponentScan<\/h4>\n<p>[java]@SpringBootApplication<br \/>\n@ServletComponentScan<br \/>\npublic class Application {<br \/>\n\tpublic static void main(String[] args) {<br \/>\n\t\tSpringApplication.run(Application.class, args);<br \/>\n\t}<br \/>\n}[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Springboot\u5f00\u53d1web\u5e94\u7528\u8fc7\u7a0b\u4e2d\u8981\u7528\u5230\u8fc7\u6ee4Filter\u548c\u76d1\u542c\u5668Listener, Springboot [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[93],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2022"}],"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=2022"}],"version-history":[{"count":2,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2022\/revisions"}],"predecessor-version":[{"id":2024,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2022\/revisions\/2024"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}