过滤器的好处:使用了过滤器,无论是Get/Post方式,接收参数时,都不需要再额外的进行转码,过滤器会自动的帮我们办这件事,乱码问题很好的解决了。
方式一:配置文件方式配置过滤器
- 创建CharacterEncodingFilter类,实现Filter接口
public class CharacterEncodingFilter implements Filter {
private String encode = "UTF-8";
@Override
public void init(FilterConfig arg0) throws ServletException {
String encoding = arg0.getInitParameter("encode");
if (encoding != null) {
encode = encoding;
}
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
// post方式设置编码格式:
request.setCharacterEncoding(encode);
response.setContentType("text/html;charset=" + encode);
arg2.doFilter(new CharacterEncodingRequest(request), arg1);
}
@Override
public void destroy() {
}
}
/**
* 对Get方式传递的请求参数进行编码
*/
class CharacterEncodingRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
public CharacterEncodingRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
/**
* 对参数重新编码
*/
@Override
public String getParameter(String name) {
String value = super.getParameter(name);
if (value == null) {
return null;
}
String method = request.getMethod();
if ("Get".equalsIgnoreCase(method)) {
try {
value = new String(value.getBytes("ISO8859-1"),
request.getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return value;
}
}
- 在web.xml中配置过滤器
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.filter.util.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encode</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
方式二:注解的方式配置过滤器。这种方式较为简单。对过滤器的配置,以注解的方式定义。
@WebFilter(initParams = { @WebInitParam(name = "encode", value = "UTF-8") }, urlPatterns = { "/*" })
public class CharacterFilter implements Filter {
private String encoding = "UTF-8";
@Override
public void init(FilterConfig arg0) throws ServletException {
String encode = arg0.getInitParameter("encode");
if (encode != null) {
encoding = encode;
}
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset=" + encoding);
arg2.doFilter(new CharacterEncodingRequest(request), arg1);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
/**
* 对Get方式传递的请求参数进行编码
*/
class CharacterEncodingRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
public CharacterEncodingRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
/**
* 对参数重新编码
*/
@Override
public String getParameter(String name) {
String value = super.getParameter(name);
if (value == null) {
return null;
}
String method = request.getMethod();
if ("Get".equalsIgnoreCase(method)) {
try {
value = new String(value.getBytes("ISO8859-1"),
request.getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return value;
}
}
欢迎大家批评指正。
点击查看更多内容
5人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦