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>

 

转载于:https://www.cnblogs.com/pangyd/p/7576943.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值