How to configure the filter filter to handle JSP garbled characters

Source: Internet
Author: User

Refer to the examples example of the Tomcat server directory WebApps

Simple configuration steps:
1. Add filter tags <filter> and <filter-mapping>; in Project Web. xml file
2. Implement the filter code;
3. Configure uriencoding for server.xml files in the Tomcat server conf directory;
4. The charset value of the foreground page setting contenttype is the same as the value set in Web. Xml.

Detailed configuration steps:
1. Configure the Web.xm file, add the filter configuration in the Web-app tab <filter> and <filter-mapping>

[HTML] view Plaincopy

<!--Chinese garbled processing filter--

    1. <filter>
    2. <filter-name>EncodingFilter</filter-name><!--Filter Name yourself--
    3. <filter-class>com.filters.SetCharacterEncodingFilter</filter-class><!--filter class names, including package names consistent, Write All---
    4. <init-param><!--initialization parameters, character set encoding to be specified--
    5. <param-name>encoding</ param-name><!--Parameter name--
    6. <param-value>GBK</param-value><!--parameter Value--
    7. </init-param>
    8. <init-param><!--initialization parameters, specifying whether to ignore case-and
    9. <param-name>ignore </param-name>
    10. <param-value>true</param-value>
    11. </init-param>
    12. </filter>
    13. <filter-mapping>
    14. <filter-name>EncodingFilter</filter-name>
    15. <url-pattern>/*</url-pattern> <!--"/*" means all resources under the project--
    16. </filter-mapping>
    17. <!--Chinese garbled processing filter-->

2. Implement the filter code to create a Setcharacterencodingfilter class

[Java] view plaincopy

  1. Package com.filters;
  2. Import java.io.IOException;
  3. Import Javax.servlet.Filter;
  4. Import Javax.servlet.FilterChain;
  5. Import Javax.servlet.FilterConfig;
  6. Import javax.servlet.ServletException;
  7. Import Javax.servlet.ServletRequest;
  8. Import Javax.servlet.ServletResponse;
  9. /**
  10. * Custom Character processing filter
  11. * Refer to from Tomcat directory Webapps\examples\web-inf\classes\filters
  12. * @author Coderlu
  13. * @since 2012-12-10
  14. */
  15. public class Setcharacterencodingfilter implements Filter {
  16. protected Filterconfig filterconfig = null; Initialize configuration
  17. protected String encoding = NULL; Receive character encoding
  18. Protected Boolean ignore = true; Whether to ignore uppercase and lowercase
  19. /* destructor
  20. * @see Javax.servlet.filter#destroy ()
  21. */
  22. @Override
  23. public void Destroy () {
  24. this.encoding = null;
  25. This.filterconfig = null;
  26. }
  27. /* Perform filtering operations
  28. * @see Javax.servlet.filter#dofilter (javax.servlet.ServletRequest, Javax.servlet.ServletResponse, Javax.servlet.FilterChain)
  29. */
  30. @Override
  31. public void DoFilter (ServletRequest request, servletresponse response,
  32. Filterchain chain) throws IOException, Servletexception {
  33. if (Ignore | | (request.getcharacterencoding () = = null)) {
  34. String encoding = selectencoding (request); If NULL is first obtained from Web. xml
  35. if (encoding! = NULL) {
  36. request.setcharacterencoding (encoding); Setting Character Set encoding
  37. }
  38. }
  39. Chain.dofilter (request, response);
  40. }
  41. /* Initialize Filter
  42. * @see Javax.servlet.filter#init (javax.servlet.FilterConfig)
  43. */
  44. @Override
  45. public void init (Filterconfig filterconfig) throws Servletexception {
  46. This.filterconfig = Filterconfig;
  47. this.encoding = Filterconfig.getinitparameter ("encoding"); Read the value of encoding from the Web. xml file
  48. String value = filterconfig.getinitparameter ("Ignore"); Read the value of ignore from the Web. xml file
  49. The following three cases are case-insensitive
  50. if (value = = null)
  51. This.ignore = true;
  52. else if (Value.equalsignorecase ("true"))
  53. This.ignore = true;
  54. else if (value.equalsignorecase ("yes"))
  55. This.ignore = true;
  56. Else
  57. This.ignore = false;
  58. }
  59. /**
  60. * Get character encoding
  61. * @param request
  62. * @return
  63. */
  64. Protected String selectencoding (ServletRequest request) {
  65. return (this.encoding);
  66. }
  67. }

3. Modify the Tomcat server Conf\server.xml file and add a uriencoding= "GBK"

[HTML] view Plaincopy

    1. <!--uriencoding Configuration Server Response encoding set--
    2. <connector port= "8080" protocol= "http/1.1"
    3. connectiontimeout= "20000"
    4. Redirectport= "8443"
    5. uriencoding= "GBK"/>

4. Setting the foreground page character encoding set

[HTML] view Plaincopy

    1. <%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>

The value of the charset here is the same as the <param-value></param-value> value in Web. xml

================================= Finish =====================================
Reference from:
Examples example of the Tomcat server directory WebApps
Filter filter configuration for JSP Chinese garbled: http://www.cnblogs.com/zhuboxingzbx/articles/1208118.html
JSP Chinese garbled, filter way to solve: http://www.zhouwenze.com/archives/20110729-411.html
JSP Chinese garbled filter Example: http://blog.csdn.net/wkupaochuan/article/details/7461208

http://blog.csdn.net/lutinghuan/article/details/8277422
========================================================================

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.