Java-filter filters are used to filter the encoding of the entire project

Source: Internet
Author: User

The entire class is divided into implementation classes and registered in the Web. xml file to write the filter class

The code is as follows
  1. Package cn.itcast.itcaststore.web.filter;
  2. Import java.io.IOException;
  3. Import java.io.UnsupportedEncodingException;
  4. Import Java.util.Map;
  5. Import Javax.servlet.Filter;
  6. Import Javax.servlet.FilterChain;
  7. Import Javax.servlet.FilterConfig;
  8. Import javax.servlet.ServletException;
  9. Import Javax.servlet.ServletRequest;
  10. Import Javax.servlet.ServletResponse;
  11. Import Javax.servlet.http.HttpServletRequest;
  12. Import Javax.servlet.http.HttpServletRequestWrapper;
  13. /**
  14. * Coded filter (for unified project coding)
  15. */
  16. public class Encodingfilter implements Filter {
  17. public void init (Filterconfig filterconfig) throws Servletexception {
  18. }
  19. public void DoFilter (ServletRequest request, servletresponse response,
  20. Filterchain chain) throws IOException, Servletexception {
  21. Processing request garbled
  22. HttpServletRequest httpservletrequest = (httpservletrequest) request;
  23. HttpServletRequest myrequest = new Myrequest (httpservletrequest);
  24. Processing response garbled
  25. Response.setcontenttype ("Text/html;charset=utf-8");
  26. Chain.dofilter (myrequest, response);
  27. }
  28. public void Destroy () {
  29. }
  30. }
  31. Customizing the Request Object
  32. Class Myrequest extends Httpservletrequestwrapper {
  33. private HttpServletRequest request;
  34. Private Boolean Hasencode;
  35. Public myrequest (HttpServletRequest request) {
  36. Super (request);//Super must write
  37. This.request = Request;
  38. }
  39. Overwrite Required enhancement methods
  40. @SuppressWarnings ({"Unchecked", "Rawtypes"})
  41. @Override
  42. Public Map Getparametermap () {
  43. Get the Request method first
  44. String method = Request.getmethod ();
  45. if (Method.equalsignorecase ("POST")) {
  46. POST request
  47. try {
  48. Handling Post garbled
  49. Request.setcharacterencoding ("Utf-8");
  50. return Request.getparametermap ();
  51. } catch (Unsupportedencodingexception e) {
  52. E.printstacktrace ();
  53. }
  54. } else if (Method.equalsignorecase ("get")) {
  55. GET request
  56. Map<string, string[]= "" > Parametermap = Request.getparametermap ();
  57. if (!hasencode) {//Make sure get manual encoding logic runs only once
  58. For (String ParameterName:parameterMap.keySet ()) {
  59. String[] values = Parametermap.get (parametername);
  60. if (values! = null) {
  61. for (int i = 0; i < values.length; i++) {
  62. try {
  63. Values[i] = new String (values[i].getbytes ("iso-8859-1"), "Utf-8");
  64. } catch (Unsupportedencodingexception e) {
  65. E.printstacktrace ();
  66. }
  67. }
  68. }
  69. }
  70. Hasencode = true;
  71. }
  72. return parametermap;
  73. }
  74. return Super.getparametermap ();
  75. }
  76. @Override
  77. public string GetParameter (string name) {
  78. @SuppressWarnings ("Unchecked")
  79. Map<string, string[]= "" > Parametermap = Getparametermap ();
  80. String[] values = parametermap.get (name);
  81. if (values = = null) {
  82. return null;
  83. }
  84. return values[0]; Retrieving the first value of a parameter
  85. }
  86. @Override
  87. Public string[] Getparametervalues (String name) {
  88. @SuppressWarnings ("Unchecked")
  89. Map<string, string[]= "" > Parametermap = Getparametermap ();
  90. String[] values = parametermap.get (name);
  91. return values;
  92. }
  93. }

Web. XML configuration

<filter>

<filter-name>encodingFilter</filter-name>

<filter-class>cn.itcast.itcaststore.web.filter.EncodingFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

Java-filter filters are used to filter the encoding of the entire project

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.