2018年7月13日 星期五

Servlet (SpringBoot 2.x 二)

有 Servle、Filter、Listener

※Servlet

※使用 annotation

@WebServlet(urlPatterns = "*.do")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("呼叫成功!");
    }
}
    
    
@SpringBootApplication
@ServletComponentScan
public class Test {
    public static void main(String[] args) {
        SpringApplication.run(Test.class, args);
    }
}

※使用 @ServletComponentScan 可以掃瞄到 @WebServlet、@Filter、@WebListener


※使用 XxxRegistrationBean

public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("呼叫成功!");
    }
}
    
    
@SpringBootApplication
public class Test {
    public static void main(String[] args) {
        SpringApplication.run(Test.class, args);
    }
    
    @Bean
    public ServletRegistrationBean<MyServlet> getServletBean() {
        return new ServletRegistrationBean<>(new MyServlet(), "*.do");
    }
}

※方法取什麼無所謂,主要是回傳值



※Filter 

※使用 annotation

※針對副檔名過濾

@WebServlet(urlPatterns = "*.do")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("呼叫成功!");
    }
}
    
    
@WebFilter(urlPatterns = "*.do")
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("過濾前");
        chain.doFilter(request, response);
        System.out.println("過濾後");
    }
    
    @Override
    public void destroy() {}
}
    
    
@SpringBootApplication
@ServletComponentScan
public class Test {
    public static void main(String[] args) {
        SpringApplication.run(Test.class, args);
    }
}



※針對 servlet 名稱過濾

@WebServlet(name="xxx", urlPatterns = "*.do")
    
    
@WebFilter(servletNames="xxx")

※和針對副檔名差不多,只有 annotation 不一樣而已,name 和 servletNames 對應好即可


※使用 XxxRegistrationBean 過濾


※針對副檔名過濾

@SpringBootApplication
public class Test {
    public static void main(String[] args) {
        SpringApplication.run(Test.class, args);
    }
    
    @Bean
    public ServletRegistrationBean<MyServlet> getServletBean() {
        return new ServletRegistrationBean<>(new MyServlet(), "*.do");
    }
    
    @Bean
    public FilterRegistrationBean<MyFilter> getFilterBean() {
        FilterRegistrationBean<MyFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new MyFilter());
        bean.addUrlPatterns("*.do");
        return bean;
    }
}



※針對 servlet 名稱過濾

public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("過濾前");
        chain.doFilter(request, response);
        System.out.println("過濾後");
    }
    
    @Override
    public void destroy() {}
}
    
    
@SpringBootApplication
public class Test {
    public static void main(String[] args) {
        SpringApplication.run(Test.class, args);
    }
    
    private ServletRegistrationBean<MyServlet> servletBean = new ServletRegistrationBean<>(new MyServlet(), "*.do");
    
    @Bean
    public ServletRegistrationBean<MyServlet> getServletBean() {
        return servletBean;
    }
    
    @Bean
    public FilterRegistrationBean<MyFilter> getFilterBean() {
        return new FilterRegistrationBean<MyFilter>(new MyFilter(), servletBean);
    }
}





※Listener

※使用 annotation

@WebListener
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextInitialized方法被呼叫了");
    }
    
    @Override
    public void contextDestroyed(ServletContextEvent sce) {}
}
    
    
@SpringBootApplication
@ServletComponentScan
public class Test {
    public static void main(String[] args) {
        SpringApplication.run(Test.class, args);
    }
}

※啟動時就會看到 contextInitialized 的內容了


※使用 XxxRegistrationBean 監聽

@SpringBootApplication
public class Test {
    public static void main(String[] args) {
        SpringApplication.run(Test.class, args);
    }
    
    @Bean
    public ServletListenerRegistrationBean<MyListener> getListenerBean() {
        return new ServletListenerRegistrationBean<>(new MyListener());
    }
}

※這種方式就不需要 MyListener 的 @WebListener 了


沒有留言:

張貼留言