Spring MVC: CharacterEncodingFilter; why just set the encoding of the response by force?

I looked at the CharacterEncodingFilter provided by Spring MVC. I was wondering why it was possible to set the response encoding when the request encoding was forcibly given by this encoding? Why not set a default response encoding if nothing is specified in the accept header fields? Or if there was no encoding in the request?

The code:

@Override 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); } 

I found this as a link https://jira.springsource.org/browse/SPR-3328?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel stating that response encoding can only be set by forced installation request encodings. Why?

Thanks in advance, Martin

+6
source share
2 answers

I can tell you what JΓΌrgen Holler says at https://jira.springsource.org/browse/SPR-3328?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel ,

Add the following filter to web.xml (Servlet 2.4+) to set the encoding:

 <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

EDIT:

CharacterEncodingFilter : Current browsers usually do not set the character encoding, even if they are specified on a page or HTML form. The above filter can either apply its encoding if the request has not yet specified an encoding, or enforces this filter encoding in any case ("forceEncoding" = "true"). If we strictly want to encode characters, we set it forcibly.

  • why was it possible to set the response encoding when the request encoding was forcibly set for this encoding?
  • Why can't I set the default response encoding if nothing is specified in the acceptance header fields? Or if there was no encoding in the request?

I think Boris's link in the commentary will answer these questions.

+5
source

If nothing works, you can create a filter (bean) in security-Context.xml and set forceEnconding = true;

 <bean id="characterEncodingFilter" class="org.springframework.web.filter.CharacterEncodingFilter"> <property name="encoding" value="utf-8"></property> <property name="forceEncoding" value="true"></property> </bean> 

Remember to set up a new custom filter:

 <security:custom-filter ref="characterEncodingFilter" after="FIRST"/> 
0
source

Source: https://habr.com/ru/post/921469/


All Articles