网站建设2017国内排行,莱芜房产论坛,中文域名解析网站,网站建设狼盾网络在Servlet 3.0之前都是使用web.xml文件进行配置#xff0c;需要增加Servlet、Filter或者Listener都需要在web.xml增加相应的配置。Servlet 3.0之后可以使用注解进行配置Servlet、Filter或者Listener#xff1b;springboot也提供了使用代码进行注册Servlet、Filter或者Listene…在Servlet 3.0之前都是使用web.xml文件进行配置需要增加Servlet、Filter或者Listener都需要在web.xml增加相应的配置。Servlet 3.0之后可以使用注解进行配置Servlet、Filter或者Listenerspringboot也提供了使用代码进行注册Servlet、Filter或者Listener。所以springboot有两种方式进行Servlet、Filter或者Listener配置。方式一使用注解(1)注册Servlet使用WebServlet注册需要在Servlet类上使用该注解即可但是需要在Configuration类中使用Spring Boot提供的注解ServletComponentScan扫描注册相应的Servlet。(2) 注册Filter使用WebFilter注册需要在Filter类上使用该注解即可但是需要在Configuration类中使用Spring Boot提供的注解ServletComponentScan扫描注册相应的Filter。(3)注册Listener使用WebListener注册需要在Filter类上使用该注解即可但是需要在Configuration类中使用Spring Boot提供的注解ServletComponentScan扫描注册相应的Listener。方式二使用spring提供的方式(1)注册Servlet使用ServletRegistrationBean注册只需要在Configuration类中加入类似以下的代码Beanpublic ServletRegistrationBean regServlet() {ServletRegistrationBean userServlet new ServletRegistrationBean();userServlet.addUrlMappings(/servlet);userServlet.setServlet(new UserServlet());return userServlet;}(2) 注册Filter使用FilterRegistrationBean注册Filter只需要在Configuration类中加入类似以下的代码Beanpublic FilterRegistrationBean regFilter() {FilterRegistrationBean userFilter new FilterRegistrationBean();userFilter .addUrlPatterns(/*);userFilter .setFilter(new UserFilter ());return userFilter ;}(3)注册Listener使用ServletListenerRegistrationBean注册Listener只需要在Configuration类中加入类似以下的代码Beanpublic ServletListenerRegistrationBean regServletListener() {ServletListenerRegistrationBean loginSessionListener new ServletListenerRegistrationBean();loginSessionListener.setListener(new LoginSessionListener());return loginSessionListener;}以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持脚本之家。