Custom filter processing mode
Custom class
public class CharactorFilter the implements Filter { // inherited Filter class // character encoding String encoding = null ; public void the doFilter (the ServletRequest request, the ServletResponse Response, the FilterChain catena alberghiera) throws IOException, ServletException { IF (! encoding = null ) { // set request character encoding Request.setCharacterEncoding (encoding); // set the response character encoding the response.setContentType ( "text / HTML; charset =" + encoding); } //Passed to the next filter the chain.doFilter (Request, Response); } public void the init (the FilterConfig FilterConfig) throws ServletException { // Get initialization parameter encoding = filterConfig.getInitParameter ( "encoding" ); } public void the destroy () { / / the TODO Auto-Generated Method Stub encoding = null ; } }
Configured in web.xml
< Filter > <-! Note that this is filter, not configured to the servlet -> < filter-name > CharactorFilter </ filter-name > <-! Filter Name -> < filter-class > cn.com. Filter.CharactorFilter </ filter-class > <-! full class name filter -> < the init-param > <-! initialization parameter -> < param-name > encoding </ param-name > <! - parameter name -> < param-value >utf-8</ Param-value > <-! Parameter -> </ the init-param > </ filter > < filter-Mapping > <-! Filter Mapping -> < filter-name > CharactorFilter </ filter- name > <-! filter name -> < url-pattern > / * </ url-pattern > <-! the uRL of maps, to handle all garbled page -> </ filter-mapping >
Use handling of SpringMVC
In web.xml configuration
< Filter > <-! Note that this is filter, not configured to the servlet -> < filter-name > CharactorFilter </ filter-name > <-! Filter Name -> < filter-class > org.springframework. web.CharacterEncodingFilter </ filter-class > < the init-param > <-! initialization parameter -> < param-name > encoding </ param-name > <-! parameter name -> < param-value > UTF -8 </param-value> <! - parameter -> </ the init-param > < the init-param > <! - initialization parameter -> < param-name > forceEncoding </ param-name > <! - Parameter name - > < param-value > to true </ param-value > <-! parameter -> </ the init-param > </ filter > < filter-mapping > <-! filter mapping -> < filter- name >CharactorFilter</ Filter-name > <-! Filter Name -> < url-pattern > / * </ url-pattern > <-! The URL of maps, to handle all garbled page -> </ filter-Mapping >
Add in springMVC profile
<Bean> tag, introduced into the object.