Java-Filter过滤器用于过滤整个项目的编码

Posted 庞玉栋的个人博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java-Filter过滤器用于过滤整个项目的编码相关的知识,希望对你有一定的参考价值。

整个分为实现类以及在web.xml文件中对编写的filter类进行注册

 

代码如下

  1. package cn.itcast.itcaststore.web.filter;
  2. import java.io.IOException;
  3. import java.io.UnsupportedEncodingException;
  4. import java.util.Map;
  5. import javax.servlet.Filter;
  6. import javax.servlet.FilterChain;
  7. import javax.servlet.FilterConfig;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.ServletRequest;
  10. import javax.servlet.ServletResponse;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletRequestWrapper;
  13. /**
  14. * 编码过滤器(用于统一项目编码)
  15. */
  16. public class EncodingFilter implements Filter {
  17. public void init(FilterConfig filterConfig) throws ServletException {
  18. }
  19. public void doFilter(ServletRequest request, ServletResponse response,
  20. FilterChain chain) throws IOException, ServletException {
  21. // 处理请求乱码
  22. HttpServletRequest httpServletRequest = (HttpServletRequest) request;
  23. HttpServletRequest myRequest = new MyRequest(httpServletRequest);
  24. // 处理响应乱码
  25. response.setContentType("text/html;charset=utf-8");
  26. chain.doFilter(myRequest, response);
  27. }
  28. public void destroy() {
  29. }
  30. }
  31. // 自定义request对象
  32. class MyRequest extends HttpServletRequestWrapper {
  33. private HttpServletRequest request;
  34. private boolean hasEncode;
  35. public MyRequest(HttpServletRequest request) {
  36. super(request);// super必须写
  37. this.request = request;
  38. }
  39. // 对需要增强方法 进行覆盖
  40. @SuppressWarnings({ "unchecked", "rawtypes" })
  41. @Override
  42. public Map getParameterMap() {
  43. // 先获得请求方式
  44. String method = request.getMethod();
  45. if (method.equalsIgnoreCase("post")) {
  46. // post请求
  47. try {
  48. // 处理post乱码
  49. request.setCharacterEncoding("utf-8");
  50. return request.getParameterMap();
  51. } catch (UnsupportedEncodingException e) {
  52. e.printStackTrace();
  53. }
  54. } else if (method.equalsIgnoreCase("get")) {
  55. // get请求
  56. Map<string, string[]=""> parameterMap = request.getParameterMap();
  57. if (!hasEncode) { // 确保get手动编码逻辑只运行一次
  58. for (String parameterName : parameterMap.keySet()) {
  59. String[] values = parameterMap.get(parameterName);
  60. if (values != null) {
  61. for (int i = 0; i < values.length; i++) {
  62. try {
  63. values[i] = new String(values[i].getBytes("ISO-8859-1"),"utf-8");
  64. } catch (UnsupportedEncodingException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. }
  69. }
  70. hasEncode = true;
  71. }
  72. return parameterMap;
  73. }
  74. return super.getParameterMap();
  75. }
  76. @Override
  77. public String getParameter(String name) {
  78. @SuppressWarnings("unchecked")
  79. Map<string, string[]=""> parameterMap = getParameterMap();
  80. String[] values = parameterMap.get(name);
  81. if (values == null) {
  82. return null;
  83. }
  84. return values[0]; // 取回参数的第一个值
  85. }
  86. @Override
  87. public String[] getParameterValues(String name) {
  88. @SuppressWarnings("unchecked")
  89. Map<string, string[]=""> parameterMap = getParameterMap();
  90. String[] values = parameterMap.get(name);
  91. return values;
  92. }
  93. }

web.xml配置

<filter>

 

<filter-name>encodingFilter</filter-name>

<filter-class>cn.itcast.itcaststore.web.filter.EncodingFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

 

</filter-mapping>

 

以上是关于Java-Filter过滤器用于过滤整个项目的编码的主要内容,如果未能解决你的问题,请参考以下文章

Filter 过滤器

Filter 过滤器

用于过滤整个字符串数组的 Ag-grid 文本过滤器

eclipse web项目的编码过滤器,统一设置成UTF-8

让你的整个项目无乱码用过滤器实现

过滤器的使用