This article takes the most common JSP + MySQL + Tomcat + Apache garbled solution as an example. We hope this will be an effective solution for your environment configuration! Garbled code has been around for a long time. In an open-source environment, garbled Code is even worse for programmers. I have experienced Garbled text in Unix (FreeBSD), so I wrote this article to help you! I classify all the garbled characters in this case into three types: 1. garbled page characters 2. garbled characters in records 3. Request transmission garbled characters The above three types of garbled characters will be parsed as follows: I. garbled page characters: 1. Case Insensitive: Org. apache. jasper. jasperexception:/top. JSP (1, 1) page directive: illegal to have multiple occurrences of contenttype with different values (old: text/html; charset = gb2312, new: text/html; charset = gb2312) 2. Inconsistent intervals: Org. apache. jasper. jasperexception:/top. JSP (1, 1) page directive: illegal to have multiple occurrences of contenttype with different values (old: text/html; charset = gb2312, new: text/html; charset = gb2312) * Solution: First, add adddefaultcharset gb2312 or adddefacharcharset GBK to Apache. Second, the unified use of the page encoding definition, such as:, OK, is indeed a mess. Now there are two solutions. * Solution: 1. Add this statement: request. setcharacterencoding ("GBK "); It can be performed on one or two pages, but this method is not a benefit. 2. register the setcharacterencodingfilter class: First, write the setcharacterencodingfilter. Java file. The Code is as follows: Package cn.com. jsp; 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. unavailableexception; Public class setcharacterencodingfilter implements filter { Protected string encoding = NULL; Protected filterconfig = NULL; Protected Boolean ignore = true; Public void destroy (){ This. Encoding = NULL; This. filterconfig = NULL; } Public void dofilter (servletrequest request, servletresponse response, Filterchain chain) throws ioexception, Servletexception { // Conditionally select and set the character encoding to be used If (ignore | (request. getcharacterencoding () = NULL )){ String encoding = selectencoding (request ); If (encoding! = NULL ){ Request. setcharacterencoding (encoding ); } } // Pass control on to the next Filter Chain. dofilter (request, response ); } 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. inclusignorecase ("true ")){ This. Ignore = true; } Else if (value. inclusignorecase ("yes ")){ This. Ignore = true; } Else { This. Ignore = false; } } Protected string selectencoding (servletrequest request ){ Return (this. Encoding ); } } This file is a request filtering class and must be registered before global compilation. The registration file is:/WEB-INF/Web. xml. Add the following code to this file: Wwwroot MySQL test app Setcharacterencodingfilter Setcharacterencodingfilter Setcharacterencodingfilter Cn.com. jsp. setcharacterencodingfilter Encoding GBK Setcharacterencodingfilter /* ...... OK. Now you can compile your setcharacterencodingfilter. Java file! At this point, Garbled text will be out of place with you! |