码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot 内嵌servlet容器的Servlets +Filters + listeners

时间:2017-11-21 18:49:34      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:over   parameter   utf-8   protected   run   img   singleton   tco   ges   

一:servlet

(1)启动类

@SpringBootApplication
@ServletComponentScan
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }
}

启动类用@ServletComponentScan注解标注

(2)继承HttpServlet

@WebServlet(name = "webServlet", urlPatterns = "/webServlet", loadOnStartup = 2)
public class WebServletTest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("########doGet###########");
        System.out.println("WebServletTest.doGet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("########doPost###########");
        System.out.println("WebServletTest.doPost");
    }
}

(3)访问http://localhost:8080/webServlet    运行结果:

技术分享图片

 

 二:Filters

测试示例1

@WebFilter(urlPatterns = "/*",filterName="CharacterEncodeFilter",
        initParams={
                @WebInitParam(name="encoding",value="UTF-8"),
                @WebInitParam(name = "forceEncoding", value = "true")
        })
@Singleton
public class CharacterEncodingFilter implements Filter {
    private String encoding = "UTF-8";
    private boolean forceEncoding = true;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.encoding = filterConfig.getInitParameter("encoding");
        String force = filterConfig.getInitParameter("forceEncoding");
        this.forceEncoding = (force == null) || Boolean.valueOf(force);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if (this.forceEncoding || servletRequest.getCharacterEncoding() == null) {
            servletRequest.setCharacterEncoding(this.encoding);
            servletResponse.setCharacterEncoding(this.encoding);
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

测试示例2

@WebFilter(urlPatterns = "/*", filterName = "indexFilter")
public class IndexFilter implements Filter {
    private final static Logger logger = LoggerFactory.getLogger(IndexFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("init IndexFilter");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        logger.info("IndexFilter.doFilter exec");
        filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {

    }
}

 

三: listeners

示例

@WebListener
public class MyServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("ServletContex初始化");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("ServletContex销毁");
    }
}

运行结果:

技术分享图片

 

Spring Boot 内嵌servlet容器的Servlets +Filters + listeners

标签:over   parameter   utf-8   protected   run   img   singleton   tco   ges   

原文地址:http://www.cnblogs.com/fdzfd/p/7874624.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!