When a colleague encounters encoding problems, he wants to make a character encoding filter that solves the entire site. The filter class and configuration are as follows:
Filter class:
<span style="font-size:12px;">package com.chaoxing.newspaper.web.filter;import java.io.IOException;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;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;import javax.servlet.http.HttpServletResponse;public class CharacterEncodingFilter implements Filter {public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {System.out.println("Filter Execution"); final HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; //Resolute the Chinese garbled code request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); chain.doFilter((ServletRequest) Proxy.newProxyInstance(this.getClass().getClassLoader(), request.getClass().getInterfaces(), new InvocationHandler(){@Overridepublic Object invoke(Object proxy, Method method, Object[] args)throws Throwable {String methodname = method.getName(); //Get the current method if(methodname.equals("getParameter")){ //Execute request.getparameter to get the result String value = (String) method.invoke(request, args); if(value==null){ return null; } if(!request.getMethod().equalsIgnoreCase("get")){ //Judge it is for get request return value; } System.out.println(value+"|||||"); //Conversion encoding returns value = new String(value.getBytes("UTF-8"),"UTF-8"); System.out.println(value+"============"); return value; } //Leave the request to execute the request return method.invoke(request, args); } } ), res);}public void init(FilterConfig fConfig) throws ServletException {System.out.println("Filter Initialization");}@Overridepublic void destroy() {System.out.println("Filter Complete");}}</span>Filter configuration:
<span style="font-size:12px;"> <!--Solve filters that are garbled on the entire site --> <filter> <filter-name>CharacterEncoding</filter-name> <filter-class>com.XXX.web.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></span>
However, when the front-end form submits the method is get, what you get is still garbled.
At the beginning, I was confused about whether the filter was written incorrectly and whether the encoding set was written incorrectly. However, after testing, there was nothing wrong. The filter was running normally.
Finally, change value.getByte("iso-8859-1","utf-8") to value.getByte("utf-8","utf-8") and get the value in the background, which is no longer garbled. Finally, Baidu went to
It is said that the default encoding set of tomcat8 is utf-8, and the default encoding set of tomcat8 before is iso-8859-1, and I suddenly realized...
Summary: The default encoding set before tomcat7 and its version is iso-8859-1, and the default character set of tomcat8 is already UTF-8, so it no longer needs to transcode the result of request.getParameter(), and there is no need to encode the encoding filter class.
The solution to the problem of invalid Filter filter writing character encoding based on tomcat8 is the entire content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.