private String encoding; //要使用的字符集,一般我们使用UTF-8(保险起见UTF-8最好)
private boolean forceRequestEncoding = false; //是否强制设置request的编码为encoding
private boolean forceResponseEncoding = false; //是否强制设置response的编码为encoding
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String encoding = getEncoding();
if (encoding != null) { //如果设置了encoding的值,则根据情况设置request和response的编码
//若设置request强制编码或request本身就没有设置编码
//则设置编码为encoding表示的值
if (isForceRequestEncoding() || request.getCharacterEncoding() == null) {
request.setCharacterEncoding(encoding);
}
//若设置response强制编码,则设置编码为encoding表示的值
if (isForceResponseEncoding()) { //请注意这行代码,下面有额外提醒
response.setCharacterEncoding(encoding);
}
}
filterChain.doFilter(request, response);
}
filterChain.doFilter(request, response);