码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-09-22 21:05:39      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:tchar   编写   eth   ram   -name   byte   character   ring   cep   

整个分为实现类以及在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过滤器用于过滤整个项目的编码

标签:tchar   编写   eth   ram   -name   byte   character   ring   cep   

原文地址:http://www.cnblogs.com/pangyd/p/7576943.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!