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--
- <filter>
- <filter-name>EncodingFilter</filter-name><!--Filter Name yourself--
- <filter-class>com.filters.SetCharacterEncodingFilter</filter-class><!--filter class names, including package names consistent, Write All---
- <init-param><!--initialization parameters, character set encoding to be specified--
- <param-name>encoding</ param-name><!--Parameter name--
- <param-value>GBK</param-value><!--parameter Value--
- </init-param>
- <init-param><!--initialization parameters, specifying whether to ignore case-and
- <param-name>ignore </param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>EncodingFilter</filter-name>
- <url-pattern>/*</url-pattern> <!--"/*" means all resources under the project--
- </filter-mapping>
- <!--Chinese garbled processing filter-->
2. Implement the filter code to create a Setcharacterencodingfilter class
[Java] view plaincopy
- Package com.filters;
- 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;
- /**
- * Custom Character processing filter
- * Refer to from Tomcat directory Webapps\examples\web-inf\classes\filters
- * @author Coderlu
- * @since 2012-12-10
- */
- public class Setcharacterencodingfilter implements Filter {
- protected Filterconfig filterconfig = null; Initialize configuration
- protected String encoding = NULL; Receive character encoding
- Protected Boolean ignore = true; Whether to ignore uppercase and lowercase
- /* destructor
- * @see Javax.servlet.filter#destroy ()
- */
- @Override
- public void Destroy () {
- this.encoding = null;
- This.filterconfig = null;
- }
- /* Perform filtering operations
- * @see Javax.servlet.filter#dofilter (javax.servlet.ServletRequest, Javax.servlet.ServletResponse, Javax.servlet.FilterChain)
- */
- @Override
- public void DoFilter (ServletRequest request, servletresponse response,
- Filterchain chain) throws IOException, Servletexception {
- if (Ignore | | (request.getcharacterencoding () = = null)) {
- String encoding = selectencoding (request); If NULL is first obtained from Web. xml
- if (encoding! = NULL) {
- request.setcharacterencoding (encoding); Setting Character Set encoding
- }
- }
- Chain.dofilter (request, response);
- }
- /* Initialize Filter
- * @see Javax.servlet.filter#init (javax.servlet.FilterConfig)
- */
- @Override
- public void init (Filterconfig filterconfig) throws Servletexception {
- This.filterconfig = Filterconfig;
- this.encoding = Filterconfig.getinitparameter ("encoding"); Read the value of encoding from the Web. xml file
- String value = filterconfig.getinitparameter ("Ignore"); Read the value of ignore from the Web. xml file
- The following three cases are case-insensitive
- 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;
- }
- /**
- * Get character encoding
- * @param request
- * @return
- */
- Protected String selectencoding (ServletRequest request) {
- return (this.encoding);
- }
- }
3. Modify the Tomcat server Conf\server.xml file and add a uriencoding= "GBK"
[HTML] view Plaincopy
- <!--uriencoding Configuration Server Response encoding set--
- <connector port= "8080" protocol= "http/1.1"
- connectiontimeout= "20000"
- Redirectport= "8443"
- uriencoding= "GBK"/>
4. Setting the foreground page character encoding set
[HTML] view Plaincopy
- <%@ 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
========================================================================