Custom character encoding Filter __ encoding

Source: Internet
Author: User

This filter is mainly for post, get submit request to intercept, set the required coding, solve the Chinese garbled problem.

The Custom character encoding filter class Encodingfilter implements the filter interface, realizes the Init, Dofilter, Destory method

Encodingfilter.java:

Import Java.io.ioexception;import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.httpservletresponse;public class Encodingfilter implements filter{private Filterconfig
 Filterconfig = null;
 
 Private String encoding = NULL; 
  Initializes public void init (Filterconfig filterconfig) throws servletexception {this.filterconfig = Filterconfig;
  
 this.encoding = Filterconfig.getinitparameter ("encoding"); public void Dofilter (ServletRequest request, servletresponse response, Filterchain Filterchain) throws IOException,
  servletexception {HttpServletRequest req = (httpservletrequest) request;
  
  HttpServletResponse res = (httpservletresponse) response;
  SYSTEM.OUT.PRINTLN ("Request Address:" +req.getrequesturi (). toString ()); SYSTEM.OUT.PRINTLN ("Default request encoding:" +req.geTcharacterencoding ()); 
  Sets the request character encoding request.setcharacterencoding (encoding); 
     
  Set Response character encoding response.setcharacterencoding (encoding);
     
    SYSTEM.OUT.PRINTLN ("Set Request after Encoding:" +req.getcharacterencoding ()); if ("Get") Equalsignorecase (Req.getmethod (). Trim () ()) {//package the request, complete the encoding conversion of the parameter in the URL req = n
    EW Gethttpservletrequestwrapper (req,encoding);
 //Execute Next filter filterchain.dofilter (req, res);
  }//Destroy public void Destroy () {this.filterconfig = null;
  
 this.encoding = null;
 Public Filterconfig Getfilterconfig () {return filterconfig;
 public void Setfilterconfig (Filterconfig filterconfig) {this.filterconfig = Filterconfig;
 
 }
 }
The GET request is packaged, the request wrapper class Gethttpservletrequestwrapper is defined, the Httpservletrequestwrapper class is inherited, and the encoding conversion of the URL parameter in the GET request is implemented.
Gethttpservletrequestwrapper.java:
Import Java.io.unsupportedencodingexception;import javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.HttpServletRequestWrapper;
 
 public class Gethttpservletrequestwrapper extends Httpservletrequestwrapper {private String encoding = "UTF-8";
 Public Gethttpservletrequestwrapper (HttpServletRequest request) {super (request);
  Public Gethttpservletrequestwrapper (httpservletrequest request,string encoding) {super (request);
 this.encoding = encoding;
  @Override Public String getparameter (string name) {string param = Super.getparameter (name);
  if (param = = null) {param = null;
  }else{param = Encodingconvert (param);
 return param; /** * encode and convert parameters in URL/public string Encodingconvert (string param) {try {return   
         New String (Param.trim (). GetBytes ("Iso-8859-1"), encoding);   
         catch (Unsupportedencodingexception e) {return param;  }   
     }
}
Configure filters in Web.xml:
<?xml version= "1.0" encoding= "UTF-8"?> <web-app version=
"2.5" xmlns= "http://java.sun.com/xml/ns/" 
 Java ee " 
 xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " 
 xsi:schemalocation=" http://java.sun.com/ Xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
    <!--custom character encoding filter-->
  < filter>
   <filter-name>EncodingFilter</filter-name>
   <filter-class> com.eyet.framework.web.filter.encodingfilter</filter-class>
   <init-param>
    <param-name >encoding</param-name>
    <param-value>UTF-8</param-value>
  </init-param>
  </filter>
  
  <filter-mapping>
   <filter-name>EncodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
 <!--custom character encoding filter end--> <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
 </welcome-file-list >


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.