java过滤器学习
作者:网络转载 发布时间:[ 2017/1/23 11:11:21 ] 推荐标签:测试开发技术 过滤器 Java
What’s the Filter
Filter也称之为过滤器,它是Servlet技术中比较激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。
如何使用Filter 创建一个Filter类,实现javax.servlet.Filter接口 实现doFilter()方法,进行拦截 在web.xml中进行配置
package com.yiyexiaoyuan.filter;
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;
/**
* Servlet Filter implementation class TestFilter
*/
//实现Filter接口
public class TestFilter implements Filter
{
/**
* Default constructor.
*/
public TestFilter()
{
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
//Filter结束时候调用
public void destroy()
{
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
//拦截主体代码
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
//去下一个Filter
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
//初始化时候调用
public void init(FilterConfig fConfig) throws ServletException
{
// TODO Auto-generated method stub
}
}
在web.xml中配置
<filter>
<display-name>TestFilter</display-name>
<filter-name>TestFilter</filter-name>
<filter-class>com.yiyexiaoyuan.filter.TestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>TestFilter</filter-name>
<url-pattern>/TestFilter</url-pattern>
</filter-mapping>
应用场景 解决中文乱码的Filter
package com.yiyexiaoyuan.filter;
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;
/**
* Servlet Filter implementation class PageEncodingFilter
*/
public class PageEncodingFilter implements Filter
{
private String encoding = "UTF-8";
protected FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
// 本过滤器默认编码是UTF-8,但也可以在web.xml配置文件里设置自己需要的编码
if (filterConfig.getInitParameter("encoding") != null)
encoding = filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest srequset, ServletResponse sresponse,
FilterChain filterChain) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest) srequset;
request.setCharacterEncoding(encoding);
filterChain.doFilter(srequset, sresponse);
}
public void destroy()
{
this.encoding = null;
}
}
web.xml配置
<filter>
<display-name>PageEncodingFilter</display-name>
<filter-name>PageEncodingFilter</filter-name>
<filter-class>com.yiyexiaoyuan.filter.PageEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PageEncodingFilter</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11