Spring MVC Filter-Character set filter (Characterencodingfilter)

Source: Internet
Author: User

Spring's character set filtering is used to handle garbled problems in projects. The filter is located in the Org.springframework.web.filter package, pointing to the class Characterencodingfilter,characterencodingfilter source code as follows:

[Java]View PlainCopy
  1. /*
  2. * Copyright 2002-2007 The original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * You are not a use this file except in compliance with the License.
  6. * Obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable or agreed to writing, software
  11. * Distributed under the License is distributed on a "as is" BASIS,
  12. * Without warranties or CONDITIONS of any KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * Limitations under the License.
  15. */
  16. Package org.springframework.web.filter;
  17. Import java.io.IOException;
  18. Import Javax.servlet.FilterChain;
  19. Import javax.servlet.ServletException;
  20. Import Javax.servlet.http.HttpServletRequest;
  21. Import Javax.servlet.http.HttpServletResponse;
  22. /**
  23. * Servlet 2.3/2.4 Filter allows one to specify a character encoding for
  24. * requests. This is useful because current browsers typically does not set a
  25. * Character encoding even if specified in the HTML page or form.
  26. *
  27. * <p>this filter can either apply its encoding if the request does not
  28. * Already specify an encoding, or enforce this filter ' s encoding
  29. * ("forceencoding" = "true"). In the latter case, the encoding would also be
  30. * Applied as Default response encoding on Servlet 2.4+ containers (although
  31. * This'll usually is overridden by a full content type set in the view).
  32. *
  33. * @author Juergen Hoeller
  34. * @since 15.03.2004
  35. * @see #setEncoding
  36. * @see #setForceEncoding
  37. * @see javax.servlet.http.httpservletrequest#setcharacterencoding
  38. * @see javax.servlet.http.httpservletresponse#setcharacterencoding
  39. */
  40. Public class Characterencodingfilter extends Onceperrequestfilter {
  41. private String encoding;
  42. Private Boolean forceencoding = false;
  43. /** 
  44. * Set the encoding to use for requests. This encoding'll is passed into a
  45. * {@link javax.servlet.http.httpservletrequest#setcharacterencoding} call.
  46. * <p>whether This encoding would override existing request encodings
  47. * (and whether it'll be applied as default response encoding as well)
  48. * Depends on the {@link #setForceEncoding "forceencoding"} flag.
  49. */
  50. public void setencoding (String encoding) {
  51. this.encoding = encoding;
  52. }
  53. /** 
  54. * Set whether the configured {@link #setEncoding encoding} of this filter
  55. * is supposed to override existing request and response encodings.
  56. * <p>default is "false", i.e. does not modify the encoding if
  57. * {@link javax.servlet.http.httpservletrequest#getcharacterencoding ()}
  58. * Returns a non-null value. Switch this to ' true ' to enforce the specified
  59. * Encoding in any case, applying it as default response encoding as well.
  60. * <p>note that the response encoding would only is set on Servlet 2.4+
  61. * containers, since Servlet 2.3 didn't provide a facility for setting
  62. * A default response encoding.
  63. */
  64. public void Setforceencoding (boolean forceencoding) {
  65. this.forceencoding = forceencoding;
  66. }
  67. @Override
  68. protected void dofilterinternal (
  69. HttpServletRequest request, HttpServletResponse response, Filterchain Filterchain)
  70. throws Servletexception, IOException {
  71. if (this.encoding! = null && (this.forceencoding | | request.getcharacterencoding () = = null) ) {  
  72. Request.setcharacterencoding (this.encoding);
  73. if (this.forceencoding) {
  74. Response.setcharacterencoding (this.encoding);
  75. }
  76. }
  77. Filterchain.dofilter (request, response);
  78. }
  79. }

The code above shows that you can set the value of two parameters when configuring a character set filter, as follows:

L Encoding: CharSet, the character set of the request to be filtered to encoding the specified value, such as UTF-8, is equivalent to:

[Java]View PlainCopy
    1. Request.setcharacterencoding

L forceencoding: Literal meaning is mandatory character set, but you do not have to understand the literal meaning, because the value of this parameter is simply to specify whether the character set of the response is also encoding the specified set of characters, so you can choose to set to TRUE or false, When the value is true, the equivalent

[Java]View PlainCopy
    1. Request.setcharacterencoding ("");
    2. Response.setcharacterencoding ("");


When the value is false, it is equivalent to:

[Java]View PlainCopy
    1. Request.setcharacterencoding ("");


The default value is False.

Example:

[Java]View PlainCopy
  1. <filter>
  2. <filter-name>characterEncodingFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class>
  4. <init-param>
  5. <param-name>encoding</param-name>
  6. <param-value>utf-8</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>forceEncoding</param-name>
  10. <param-value>true</param-value>
  11. </init-param>
  12. </filter>
  13. <filter-mapping>
  14. <filter-name>characterEncodingFilter</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>

The above code is placed in Web. XML, equivalent to the servlet:

[Java]View PlainCopy
      1. Request.setcharacterencoding ("UTF-8");
      2. Response.setcharacterencoding ("UTF-8");

Spring MVC Filter-Character set filter (Characterencodingfilter)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.