1.什么是Filter过滤器
1.Filter过滤器它是javaweb的三大组件之一,三大组件分别是:servlet程序,listener监听器,Filter过滤器
2.Filter过滤器它是javaEE的规范.也就是接口
3.Filter过滤器它的作用是: 拦截请求,过滤响应.
拦截请求常见的应用场景有: 权限检查 , 日记操作 , 事务管理 …等等
2.Filter过滤器使用
1.编写一个类去实现Filter接口
2.实现过滤方法doFilter()
3.到web.xml中配置Filter()的拦截路径
public class AdminFilter implements Filter {
public AdminFilter() {
System.out.println("1.Filter构造器方法AdminFilter()");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("2.Filter的init(FilterConfig filterConfig)初始化");
//1获取filter的名称filter-name的内容
System.out.println("filter-name的值是:"+filterConfig.getFilterName());
//2获取在web.xml中配置的init-param初始化参数
System.out.println("初始化参数username的值是:"+filterConfig.getInitParameter("username"));
System.out.println("初始化参数url的值是:"+filterConfig.getInitParameter("url"));
//3获取ServletContext对象
System.out.println(filterConfig.getServletContext());
}
/**
* doFilter方法,专门用来拦截请求,过滤响应
* @param servletRequest
* @param servletResponse
* @param filterChain
* @throws IOException
* @throws ServletException
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("3.Filter的doFilter()拦截方法");
HttpServletRequest httpServletRequest= (HttpServletRequest) servletRequest;
HttpSession session=httpServletRequest.getSession();
Object user=session.getAttribute("user");
//如果等于null,说明还没有登录
if (user==null) {
servletRequest.getRequestDispatcher("/login.jsp").forward(servletRequest, servletResponse);
return;
}else {
//让程序继续往下访问用户的目标资源
filterChain.doFilter(servletRequest,servletResponse);
}
}
@Override
public void destroy() {
System.out.println("4.Filter的destroy()销毁方法");
}
}
<!-- filter标签用于配置一个filter过滤器-->
<filter>
<!-- 给filter起一个别名-->
<filter-name>AdminFilter</filter-name>
<!-- 配置filter的全类名-->
<filter-class>com.example.Filter.AdminFilter</filter-class>
<init-param>
<param-name>username</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost3306/test</param-value>
</init-param>
</filter>
<!-- filter-mapping配置filter过滤器的拦截路径-->
<filter-mapping>
<!-- filter-name表示当前的拦截路径给哪个filter使用-->
<filter-name>AdminFilter</filter-name>
<!-- url-pattern配置拦截路径
/表示请求地址为:http://ip:port/工程路径/ 映射到IDEA的web目录
/admin/* 表示请求地址为:http://ip:port/工程路径/admin/*
-->
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.example.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
测试页面:
先准备要过滤的文件夹admin
admin文件夹放几个页面测试有没有过滤
在admin文件夹外面创建login.jsp 放入以下代码进行测试
<body>
这是登录界面login.jsp页面<br>
<form action="loginServlet" method="get">
用户名 <input type="text" name="username"/> <br>
密码 <input type="password" name="password"/> <br>
<input type="submit">
</form>
</body>
3.Filter过滤器的生命周期
Filter的生命周期包含几个方法:
1、构造器方法
2、init初始化方法
第1,2步,在web工程启动的时候执行(Filter已经创建)
3、doFilter过滤方法
第3步,每次拦截到请求,就会执行
4、destroy销毁
第4步,停止web工程的时候,就会执行(停止 web工程,也会销毁Filter过滤器
↓测试↓
public class AdminFilter implements Filter {
public AdminFilter() {
System.out.println("1.Filter构造器方法AdminFilter()");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("2.Filter的init(FilterConfig filterConfig)初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("3.Filter的doFilter()拦截方法");
}
@Override
public void destroy() {
System.out.println("4.Filter的destroy()销毁方法");
}
}
4.Filter过滤器执行的细节
测试代码:
Filter1:
public class Filter1 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("filter1 前置代码");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("filter1 后置代码");
}
@Override
public void destroy() {
}
}
Filter2:
public class Filter2 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("filter2 前置代码");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("filter2 后置代码");
}
@Override
public void destroy() {
}
}
xml配置:
<filter>
<filter-name>Filter1</filter-name>
<filter-class>com.example.Filter.Filter1</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter1</filter-name>
<url-pattern>/target.jsp</url-pattern>
</filter-mapping>
<filter>
<filter-name>Filter2</filter-name>
<filter-class>com.example.Filter.Filter2</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter2</filter-name>
<url-pattern>/target.jsp</url-pattern>
</filter-mapping>
5.Filter的拦截路径
–精确匹配
/target.jsp
以上配置的路径,表示请求地址必须为:http://ip:port/工程路径/target.jsp
–目录匹配
/admin/
以上配置的路径,表示请求地址必须为: http://ip:port/工程路径/admin/
–后缀名匹配
*.html
以上配置的路径,表示请求地址必须为.html结尾才会拦截
*.do
以上配置的路径,表示请求地址必须为.do结尾才会拦截
*.action
以上配置的路径,表示请求地址必须为.action结尾才会拦截