Java character encoding filter configuration source code

Source: Internet
Author: User
Package COM. common. utils; import javax. servlet. *; import Java. io. ioexception; public class setcharacterencodingfilter implements filter {protected string encoding = NULL; // The encoding method protected filterconfig = NULL; // The parameter configuration object rotected Boolean ignore = true; // whether encoding is used. // read the public void Init (filterconfig) throws servletexception {This. filterconfig = filterconfig; this. encoding = Filterconfig. getinitparameter ("encoding"); string value = filterconfig. getinitparameter ("Ignore"); If (value = NULL) This. ignore = true; else if (value. equalsignorecase ("true") This. ignore = true; else if (value. equalsignorecase ("yes") This. ignore = true; else this. ignore = false;} // filter processing. If ignore is true, the specified public void dofilter (servletrequest request, servletresponse response, filterchai N chain) throws ioexception, servletexception {// use if (ignore | (request. getcharacterencoding () = NULL) {string encoding = This. encoding; If (encoding! = NULL) request. setcharacterencoding (encoding);} chain. dofilter (request, response);} // empty parameter object public void destroy () {This. encoding = NULL; this. filterconfig = NULL;} web. xml configuration: <filter> <filter-Name> setcharacterencodingfilter </filter-Name> <filter-class> COM. web0248.filter. setcharacterencodingfilter </filter-class> <init-param> <! -- Define the encoding format, I use UTF-8 --> <param-Name> encoding </param-Name> <param-value> UTF-8 </param-value> </init-param> <init- param> <! -- The ignore parameter is defined in the filter class --> <param-Name> ignore </param-Name> <param-value> true </param-value> </init-Param> </filter> <filter-mapping> <filter-Name> setcharacterencodingfilter </filter-Name> <servlet-Name> action </servlet-Name> </filter-mapping> <filter-mapping> <filter-Name> setcharacterencodingfilter </filter-Name> <servlet-Name> *. JSP </servlet-Name> </filter-mapping>}

Note: In the Web. xml file, all filter elements must appear before any filter-mapping element, and the filter-mapping element must appear before all servlet or servlet-mapping elements.

The order from top to bottom is:... → filter-mapping → listener → servlet-mapping →...

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.