Filter
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Filter相关的知识,希望对你有一定的参考价值。
1.什么是Filter
① JavaWeb的一个重要组件,可以对发送到Servlet的请求进行拦截,并对响应也进行拦截。
② Filter是实现了Filter接口的Java类
③ Filter 需要在web.xml文件中进行配置和映射
2.如何创建一个Filter,并跑起来
①.创建一个Filter类:实现Filter接口:public class MyFilter implements Filter
②.在web.xml文件中配置并映射该Filter
<!-- 注册Filter --> <filter> <filter-name>firstFilter</filter-name> <filter-class>com.java.testFilter.MyFilter</filter-class> </filter> <!-- 映射Filter --> <filter-mapping> <filter-name>firstFilter</filter-name> <url-pattern>/test.jsp</url-pattern> </filter-mapping>
3.Filter的API
Filter接口:
@Override
public void destroy()
释放当前Filter 所占用的资源的方法.在Filter被销毁之前调用,且只被调用一次.
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain filterChain) throws IOException, ServletException
真正Filter的逻辑代码需要写在该方法中.每次拦截都会调用该方法.
*FilterChain : Filter链,多个Filter可以构成一个Filter链.
filterChain.doFilter(ServletRequest arg0, ServletResponse arg1) 把请求传给Filter链的下一个
Filter,若当前Filter是Filter链的最后一个Filter,将把请求给到目标Servlet(或JSP)
*多个Filter的拦截顺序和<filter-mapping>的配置顺序有关,靠前的先被调用.
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("init...");
}
//类似于Servlet的init方法.在创建Filter对象(Filter对象被web应用加载时立即被调用,
且只被调用一次.该方法对于当前的Filter进行初始化操作.Filter实例是单例的)
*FilterConfig 类似于 ServletConfig
*可以在web.xml文件中配置当前Filter 的初始化参数.配置方式也和Servlet类似.
<!-- 注册Filter --> <filter> <filter-name>firstFilter</filter-name> <filter-class>com.java.testFilter.MyFilter</filter-class> <init-param> <param-name>name</param-name> <param-value>java</param-value> </init-param> </filter>
*与开发Servlet不同的是,Filter 接口并没有相应的实现类可以继承,要开发过滤器只能实现接口
同一个web.xml文件中可以为同一个Filter设置多个映射
example:
一个登陆界面
<font color="red">${message }</font> <br><br> <form action="${pageContext.request.contextPath }/hello.jsp" method="post"> UserName:<input type="text" name="username" value="${param.username }"> <br> Password :<input type="password" name="password"> <br> <input type="submit" value="submit"> </form>
两个过滤器
package com.java.testFilter; import java.io.IOException; 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.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 拦截/hello.jsp,如果username 不是Tom 则转发到login.jsp,否则放行 */ public class UserNameFilter implements Filter { public UserNameFilter() { } public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String initUser = filterConfig.getInitParameter("username"); String username = request.getParameter("username"); if(!initUser.equals(username)){ request.setAttribute("message", "用户名错误!"); request.getRequestDispatcher("/login.jsp").forward(request, response); return; } chain.doFilter(request, response); } private FilterConfig filterConfig; public void init(FilterConfig fConfig) throws ServletException { this.filterConfig = fConfig; } }
package com.java.testFilter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; /** * 若密码不是1234,则把请求给login.jsp,否则给目标页面 */ public class PasswordFilter implements Filter { public PasswordFilter() { // TODO Auto-generated constructor stub } public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { ServletContext servletContext = filterConfig.getServletContext(); String initPass = servletContext.getInitParameter("password"); String password = request.getParameter("password"); if(!initPass.equals(password)){ request.setAttribute("message", "密码错误!"); request.getRequestDispatcher("/login.jsp").forward(request, response); return; } chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ private FilterConfig filterConfig; public void init(FilterConfig fConfig) throws ServletException { this.filterConfig = fConfig; } }
web.xml配置
<context-param> <param-name>password</param-name> <param-value>1234</param-value> </context-param> <filter> <display-name>UserNameFilter</display-name> <filter-name>UserNameFilter</filter-name> <filter-class>com.java.testFilter.UserNameFilter</filter-class> <init-param> <param-name>username</param-name> <param-value>Tom</param-value> </init-param> </filter> <filter-mapping> <filter-name>UserNameFilter</filter-name> <url-pattern>/hello.jsp</url-pattern> </filter-mapping> <filter> <display-name>PasswordFilter</display-name> <filter-name>PasswordFilter</filter-name> <filter-class>com.java.testFilter.PasswordFilter</filter-class> </filter> <filter-mapping> <filter-name>PasswordFilter</filter-name> <url-pattern>/hello.jsp</url-pattern> </filter-mapping> <servlet> <description></description> <display-name>TestServlet2</display-name> <servlet-name>TestServlet2</servlet-name> <servlet-class>com.java.testFilter.TestServlet2</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet2</servlet-name> <url-pattern>/TestServlet2</url-pattern> </servlet-mapping>
hello.jsp
<h4>Hello Page</h4> Hello:${param.username }
以上是关于Filter的主要内容,如果未能解决你的问题,请参考以下文章
php代码片段: sendFile/videoStream/sendEmail/phpexcel/ffmpeg/zip
text 此片段用于以下知识库文章 - http://kb.wpbeaverbuilder.com/article/591-create-a-filter-to-customize-the-di