返回值类型 | 方法 | 功能描述 |
---|---|---|
void | init (FilterConfig filterConfig) | 该方法用于初始化过滤器。 |
void | doFilter(ServletRequest request,SeivletResponse response, FilterChain chain) |
该方法完成实际的过滤操作,当客户端请求的 URL 与过滤器映射的 URL 匹配时,容器会先调用该方法对请求进行拦截。 参数 request 和 response 表示请求和响应对象。 参数 chain 代表当前 Filter 链对象,在该方法内部,调用 chain.doFilter() 方法,才能把请求交付给 Filter 链中的下一个 Filter 或者 Web 资源。 |
void | destroy() | 该方法在销毁 Filter 对象之前被调用,用于释放被 Filter 对象占用的资源。 |
<filter> <filter-name>myFilter</filter-name> <filter-class>net.biancheng.www.MyFilter</filter-class> <init-param> <param-name>name</param-name> <param-value>编程帮</param-value> </init-param> <init-param> <param-name>URL</param-name> <param-value>www.lmcjl.com</param-value> </init-param> </filter>
<filter-mapping> <filter-name>myFilter</filter-name> <url-pattern>/login</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> <filter-mapping> <filter-name>myFilter</filter-name> <servlet-name>ServletDemo</servlet-name> </filter-mapping>
属性名 | 类型 | 描述 |
---|---|---|
filterName | String | 指定过滤器的 name 属性,等价于 <filter-name>。 |
urlPatterns | String[] | 指定过滤器的 URL 匹配模式。等价于 <url-pattern> 标签。 |
value | String[] | 该属性等价于 urlPatterns 属性,但是两者不能同时使用。 |
servletNames | String[] | 指定过滤器将应用于哪些 Servlet。取值是 @WebServlet 中 filterName 属性的取值,或者 web.xml 中 <servlet-name> 的取值。 |
dispatcherTypes | DispatcherType | 指定过滤器拦截的资源被 Servlet 容器调用的方式。具体取值包括: ASYNC、ERROR、FORWARD、INCLUDE、REQUEST。 |
initParams | WebInitParam[] | 指定一组过滤器初始化参数,等价于 <init-param> 标签。 |
asyncSupported | boolean | 声明过滤器是否支持异步操作模式,等价于 <async-supported> 标签。 |
description | String | 指定过滤器的描述信息,等价于 <description> 标签。 |
displayName | String | 指定过滤器的显示名,等价于 <display-name> 标签。 |
package net.biancheng.www.filter; import java.io.IOException; import java.util.logging.LogRecord; import javax.servlet.DispatcherType; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; @WebFilter( dispatcherTypes = { DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ERROR }, asyncSupported = true, description = "过滤器4", urlPatterns = {"/login"}, initParams = { @WebInitParam(name = "name", value = "编程帮", description = "name的描述") }, servletNames = {"SuccessServlet"}) public class MyFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { } }
package net.biancheng.www.filter; import java.io.IOException; import javax.servlet.DispatcherType; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; /** * 第一个 Filter 过滤器 * * @author 编程帮 www.lmcjl.com */ @WebFilter(dispatcherTypes = { DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ERROR } , urlPatterns = {"/FilterServlet"}) public class MyFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //设置向页面输出的格式与编码 response.setContentType("text/html;charset=UTF-8"); //通过Filter向页面输出内容 response.getWriter().write("<h1>hello MyFilter</h1>"); } public void init(FilterConfig fConfig) throws ServletException { } }
package net.biancheng.www.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Filter 过滤器拦截的 Servlet * @author 编程帮 www.lmcjl.com */ @WebServlet("/FilterServlet") public class FilterServlet extends HttpServlet { private static final long serialVersionUID = 1L; public FilterServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置向页面输出的格式与编码 response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("<h3>FilterServlet访问成功</h3>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
本文链接:http://task.lmcjl.com/news/6528.html