Spring Boot(二)整合web开发Servlet、Fil

2020-08-05  本文已影响0人  honest涛

作者:毕涛涛,致力于Java学习的践行者。原创文章,转载请注明出处。

整合web开发

1.整合Servlet
2.整合Filter
3.整合Listener

一、整合Servlet

通过注解扫描完成Servlet组件的注册

1.编写Servlet

package com.deewinkg.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * SpringBoot整合Servlet方式一
 * @author DY
 *
 * <servlet>
 *  <servlet-name>FirstServlet</servlet-name>
 *  <servlet-class>com.deewinkg.servlet.FirstServlet</servlet-class>
 * </servlet>
 *
 * <servlet-mapping>
 *  <servlet-name>FirstServlet</servlet-name>
 *  <url-pattern>/first</url-pattern>
 * </servlet-mapping>
 *
 */
@WebServlet(name = "FirstServlet",urlPatterns = "/first")
public class FirstServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.print("FirstServlet...");
    }
}

2.编写启动类

package com.deewinkg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
 * SpringBoot整合Servlet方式一
 * @author DY
 */
@SpringBootApplication
@ServletComponentScan //在springBoot启动时会扫描@WebServlet,并将该类实例化
public class App1 {
    public static void main(String[] args) {
        SpringApplication.run(App1.class,args);
    }
}
3.运行结果: 通过注解扫描servlet组件.png

通过方法完成Servlet组件的注册

1.编写Servlet

package com.deewinkg.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * SpringBoot整合Servlet方式二
 * @author DY
 */
public class SecondServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.print("SecondServlet...");
    }
}

2.编写启动类

package com.deewinkg;

import com.deewinkg.servlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class App2 {
    public static void main(String[] args) {
        SpringApplication.run(App2.class,args);
    }
    @Bean
    public ServletRegistrationBean getServletRegistrationBean()
    {
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/secondServlet");
        return bean;
    }
}
3.运行结果: 通过方法完成servlet组件的注册.png

二、整合Filter

通过注解扫描完成Filter组件的注册

1.编写Filter

package com.deewinkg.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * SpringBoot 整合 Filter 方式一
 * @author DY
 * <filter>
 *  <filter-name>FirstFilter</filter-name>
 *  <filter-class>com.deewinkg.filter.FirstFilter</filter-class>
 * </filter>
 * <filter-mapping>
 *  <filter-name>FirstFilter</filter-name>
 *  <url-pattern>/first</url-pattern>
 * </filter-mapping>
 */
//@WebFilter(filterName="FirstFilter",urlPatterns={"*.do","*.jsp"})
@WebFilter(filterName ="FirstFilter",urlPatterns = "/first")
public class FirstFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入Filter");
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("离开Filter");
    }

    @Override
    public void destroy() {

    }
}

2.编写启动类

package com.deewinkg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
 * SpringBoot整合Filter方式一
 * @author DY
 */
@SpringBootApplication
@ServletComponentScan //在springBoot启动时会扫描@WebServlet,并将该类实例化
public class App1 {
    public static void main(String[] args) {
        SpringApplication.run(App1.class,args);
    }
}
3.运行结果: 通过注解扫描filter组件.png

通过方法完成Filter组件的注册

1.编写filter

package com.deewinkg.filter;

import javax.servlet.*;
import java.io.IOException;

public class SecondFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("Filter Start...");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("Filter End...");
    }

    @Override
    public void destroy() {

    }
}

2.编写启动类

package com.deewinkg;

import com.deewinkg.filter.SecondFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 *  SpringBoot 整合 Filter 方式二
 */
@SpringBootApplication
public class App2 {
    public static void main(String[] args) {
        SpringApplication.run(App2.class,args);
    }
    /**
     * 注册 Filter
     * @return
     */
    @Bean
    public FilterRegistrationBean getFilterRegistrationBean()
    {
        FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
        //bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
        bean.addUrlPatterns("/secondFilter");
        return bean;
    }
}
3.远行结果 通过方法完成filter组件注册.png

三、整合listener

通过注解扫描完成listener组件的注册

1.编写listener

package com.deewinkg.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * SpringBoot整合listener方式一
 * <listener>
 *     <listener-class>com.deewinkg.listener.FirstListener</listener-class>
 * </listener>
 */
@WebListener
public class FirstListener implements ServletContextListener
{
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Listener...init......");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

2.编写启动类

package com.deewinkg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
 * SpringBoot整合Filter方式一
 * @author DY
 */
@SpringBootApplication
@ServletComponentScan
public class App1 {
    public static void main(String[] args) {
        SpringApplication.run(App1.class,args);
    }
}
3.运行结果 通过注册扫描完成listener组件的注册.png

通过方法完成Listener组件的注册

1.编写listener

package com.deewinkg.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * SpringBoot整合listener方式二
 */
public class SecondListener implements ServletContextListener
{
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("......listener组件初始化......");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

2.编写启动类

package com.deewinkg;

import com.deewinkg.listener.SecondListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 *  SpringBoot 整合 Filter 方式二
 */
@SpringBootApplication
public class App2 {
    public static void main(String[] args) {
        SpringApplication.run(App2.class,args);
    }

    /**
     * 注册listener
     * @return
     */
    @Bean
    public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean()
    {
        ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
        return bean;
    }
}

3.运行结果


通过方法完成Listener组件的注册.png
上一篇下一篇

猜你喜欢

热点阅读