CharacterEncodingFilter的坑

不少時候,咱們覺得配置了CharacterEncodingFilter就能解決亂碼問題,實際上不是的,java

咱們能夠觀察其doFilterInternal方法this

protected void doFilterInternal(
			HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {

		if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
			request.setCharacterEncoding(this.encoding);
			if (this.forceEncoding) {
                //這裏只設置了編碼字符
				response.setCharacterEncoding(this.encoding);
			}
		}
		filterChain.doFilter(request, response);
	}

響應的話還須要設置內容類型,好比:編碼

response.setContentType(MediaType.TEXT_HTML);code

相關文章
相關標籤/搜索