A solution to the problem of invalid Filter filter based on tomcat8
- 2020-12-13 18:59:19
- OfStack
When colleagues encounter coding problems, they want to make a character coding filter to solve the whole station. The class and configuration of the filter 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;
// To solve post The Chinese character of the request is confused
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(){
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodname = method.getName(); // Get the current method
if(methodname.equals("getParameter")){
// perform request.getparameter To get the results
String value = (String) method.invoke(request, args);
if(value==null){
return null;
}
if(!request.getMethod().equalsIgnoreCase("get")){ // Judgment is for get request
return value;
}
System.out.println(value+"|||||");
// Conversion code return
value = new String(value.getBytes("UTF-8"),"UTF-8");
System.out.println(value+"=======");
return value;
}
// to request Perform the requested
return method.invoke(request, args);
}
} ), res);
}
public void init(FilterConfig fConfig) throws ServletException {
System.out.println(" Filter initialization ");
}
@Override
public void destroy() {
System.out.println(" Filter completion ");
}
}</span>
Filter configuration:
<span style="font-size:12px;"> <!-- Solve the whole station disorderly code filter -->
<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, in the case that the front-end form is submitted as get, the result is still messy.
At the beginning, 1 was obsessed with whether the filter was wrong and the code set was wrong. However, after testing, there was no mistake. The filter worked normally.
Finally, change value.getByte (" ES17en-8859-1 "," utf-8 ") to ES19en.getByte (" ES21en-8 "," utf-8 ") to get the value. And then at the end of the day,
The default code set of tomcat8 is utf-8, and the default code set before tomcat8 is iso-8859-1.
Summary: The default encoding set before tomcat7 and its version is iso-8859-1, and the default character set of tomcat8 is es34EN-8, so transcoding for the results of ES35en.getParameter () is no longer required, and the encoding filter class is no longer required