过滤器
1:过滤器的作用
把通用的、相同的处理代码用过滤器实现,可以共享,然后在web.xml中将过滤器配置给相关的资源使用即可
2:过滤器的开发方法
- 写一个类实现Filter接口
- 重写Filter接口的三个方法
- IDE开发工具开发过滤器的步骤:
- 在doFilter()方法写逻辑: System.out.println("MyFilter.doFilter()...");
- 创建servlet,运行servlet测试.
- 放行 执行chain.doFilter(request,response)
- 不放行 不执行chain.doFilter(request,response)
package com.tjetc.filter;
import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;
public class HelloFilter implements Filter {
public HelloFilter() {
System.out.println("HelloFilter的构造方法");
}
/**
* 初始化, 调用一次, Tomcat启动时会调用Filter的init ?
*
* @param filterConfig
* @throws ServletException
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("HelloFilter的init方法被调用");
String filterName = filterConfig.getFilterName();
System.out.println("filterName=" + filterName);
Enumeration<String> initParameterNames = filterConfig.getInitParameterNames();
while (initParameterNames.hasMoreElements()) {//initParameterNames.hasMoreElements())遍历过程中判断是否还有元素遍历
//获取参数名称
String parameterName = initParameterNames.nextElement();
//获取参数对应的value值
String parameterValue = filterConfig.getInitParameter(parameterName);
System.out.println(parameterName + "=" + parameterValue);
}
ServletContext servletContext = filterConfig.getServletContext();
//获取上下文对象
System.out.println("servletContext=" + servletContext);
}
/**
* 每次Filter拦截到的请求, 都会调用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("HelloFilter的doFilter方法被调用");
//例如:验证是否的登录过,没有登录,返回到登录页面或者返间json, 需要的登录
//filterChain 过滤器链, 不处理过滤器链, 那就认为链条断掉, 后续的filter和servlet都不会访问
//filterChain.doFilter(servletRequest,servletResponse);
filterChain.doFilter(servletRequest, servletResponse);//放行,执行后续的filter和servlet
}
/**
* 销毁Filter时调用, 调用一次, 使用场景:释放资源
*/
@Override
public void destroy() {
System.out.println("HelloFilter的destroy方法被调用");
}
}
web.xml配置HelloFilter
<filter>
<filter-name>HelloFilter</filter-name>
<filter-class>com.tjetc.filter.HelloFilter</filter-class>
<init-param>
<param-name>age</param-name>
<param-value>19</param-value>
</init-param>
<init-param>
<param-name>email</param-name>
<param-value>ww@163.com</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>HelloFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
3、过滤器的各个方法执行顺序
- 构造方法
- init(filterConfig)初始化方法
- doFilter()方法
- destroy()方法
package com.tjetc.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;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// resp.setContentType("text/html;charset=utf-8");
// resp.getWriter().write("<h1>HelloServlet欢迎您!</h1>");
//请求转发到my.jsp页面
req.getRequestDispatcher("/my.jsp").forward(req, resp);
}
}
web.xml配置HelloServlet
<servlet>
<servlet-name>HelloServlet<