上一篇文章中咱们讲到了快速搭建Springboot项目以及整合Mybatis,可是在实际的开发中咱们还须要用到不少的东西。后面的文章中咱们会陆续的加入各个知识点,慢慢的丰满咱们的demo。html
Filter的用法java
咱们先来了解下Filterweb
Filter也称之为过滤器,它是Servlet技术中最实用的技术,Web开发人员经过Filter技术,对web服务器管理的全部web资源:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。spring
它主要用于对用户请求进行预处理,也能够对HttpServletResponse进行后处理。使用Filter的完整流程:Filter对用户请求进行预处理,接着将请求交给Servlet进行处理并生成响应,最后Filter再对服务器响应进行后处理。springboot
import org.springframework.core.annotation.Order; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * 使用@WebFilter注解标注过滤器 * 属性filterName声明过滤器的名称,可选 * 属性urlPatterns指定要过滤 的URL模式,也可以使用属性value来声明.(指定要过滤的URL模式是必选属性) * @Order(1)控制加载顺序 * @author 刘铖 * @since 2018/4/9 */ @Order(1) //重点 @WebFilter(filterName = "myfilter", urlPatterns = "/*") public class IndexFilter 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.print("================须要过滤器作什么?=================="); if("2".equals(servletRequest.getParameter("id"))){ servletResponse.getWriter().write("Cannot query the data!"); }else{ filterChain.doFilter(servletRequest,servletResponse); } } @Override public void destroy() {} }
过滤器写好了该如何使用呢?还须要配置web.xml吗?服务器
在springboot中使用过滤器不需咱们在去配置web.xml了,那如何让程序扫描到它呢以下session
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; /** * 使用 @ServletComponentScan注解后,Servlet、Filter、Listener * 能够直接经过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其余代码。 * @author 刘铖 * @since 2018-04-09 **/ @SpringBootApplication //重点 @ServletComponentScan public class ABountyNetApplication1 { public static void main(String[] args) { SpringApplication.run(ABountyNetApplication1.class, args); } }
这样咱们就完成了一个简单拦截器。ide
重点就是两个注解 @ServletComponentScan @WebFilter顺带也能够去了解一下@order网站
Listener的用法url
监听器用于监听web应用中某些对象、信息的建立、销毁、增长,修改,删除等动做的发生,而后做出相应的响应处理。当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法。经常使用于统计在线人数和在线用户,系统加载时进行信息初始化,统计网站的访问量等等。
按监听的对象划分,能够分为
ServletContext对象监听器
HttpSession对象监听器
ServletRequest对象监听器
按监听的事件划分
对象自身的建立和销毁的监听器
对象中属性的建立和消除的监听器
session中的某个对象的状态变化的监听器
由于分类比较多咱们只实现一种
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; /** * 使用@WebListener注解,实现ServletContextListener接口 * @author 刘铖 * @since 2018/4/9 */ @WebListener public class IndexListener implements ServletContextListener{ @Override public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("ServletContex初始化"); System.out.println(servletContextEvent.getServletContext().getServerInfo()); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { System.out.println("ServletContex销毁"); } }
如何使用呢和过滤器同样,也不须要在去配置web.xml
重点 @WebListener