1, the most basic garbled problem. This garbled problem is the simplest garbled problem. General Xinhui appears. is the page encoding inconsistency caused by garbled. <%@ page language= "java" pageencoding= "UTF-8"%> <%@ page contenttype= "Text/html;charset=iso8859-1"%> <title> Chinese issues </title> <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "> <body> I'm a good man. </body> Three places of code. The first place in the encoding format is the storage format of the JSP file. Ecljpse will save the file based on this encoding format. and compile the JSP file, including the Chinese characters inside. The second encoding is the decoding format. Because the file saved as UTF-8 is decoded to iso8859-1, so the Chinese must be garbled. That must be the same. And the second place in this line, can not. The default is also the encoding format using ISO8859-1. So if there is no such a line, "I am a good person" will also appear garbled. Must be consistent. The third code is to control how the browser is decoded. This encoding format is not related if the previous decoding is consistent and error-free. Some pages are garbled because the browser cannot determine which encoding format to use. Because pages are sometimes embedded in the page, the browser confuses the encoding format. There was garbled characters. 2, the form uses post to submit after the received garbled problem This problem is also a common problem. This garbled is also tomcat internal encoding format iso8859-1 in trouble, that is, when the post submission, if not set the encoding format of the submission, it will be submitted in iso8859-1 manner, the accepted JSP is Utf-8 accepted. causes garbled characters. Since this is the reason, there are several workarounds and comparisons. A, encoding conversion when parameters are accepted String str = new String (Request.getparameter ("something"). GetBytes ("Iso-8859-1"), "Utf-8"); In this case, each parameter must be transcoded in this way. Very troublesome. But you can actually get the kanji. B, at the beginning of the request page, execute the requested encoding code, request.setcharacterencoding ("UTF-8"), and set the character set of the submission to UTF-8. In this case, the page that accepts this parameter does not have to be transcoded. Direct use String str = request.getparameter ("Something"), the Chinese character parameter can be obtained. But every page needs to execute this sentence. This method is also effective for post submissions, which is not valid for enctype= "Multipart/form-data" when a get commits and uploads a file. The following is a separate description of the two garbled cases later. C, in order to avoid writing request.setcharacterencoding ("UTF-8") on every page, we recommend using filters for all JSP for encoding processing. There are many examples of this online. Please check them yourself. Use of/*********************************************************** filter ********************************************* **********/ Now the common garbled problem is divided into JSP page display Chinese garbled, form submission garbled two categories. 1) The JSP page displays Chinese garbled characters Use the page command in a JSP file to specify the MIME type of the response result, such as <%@ page language= "java" contenttype= "text/html;charset=gb2312"%> 2) Form submission garbled Form submission (post and Get method), using the Request.getparameter method to get garbled, this is because Tomcat processing parameters submitted by default is Iso-8859-1, form submission get and post processing garbled problems, respectively, described below. (1) Post processing The form submitted by the Post is solved by writing a filter, and the filter is called before the user submits the data, which can be changed by the code of the filter, as follows: Java code Characterencodingfilter.java: public class Characterencodingfilter implements Filter { protected String encoding = NULL; public void init (Filterconfig filterconfig) throws Servletexception { this.encoding = Filterconfig.getinitparameter ("encoding"); } public void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, Servletexception { request.setcharacterencoding (encoding); Response.setcontenttype ("text/html;charset=" +encoding); Chain.dofilter (request, response); } } Xml: <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>net.vschool.web.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> (2) Handling of Get methods Tomcat handles post and get differently, so the filter does not solve the get garbled problem, it needs to be set elsewhere. Open the Server.xml file in the <tomcat_home>\conf directory, locate the settings section of the connector component for service on port 8080, and add a property to this component: Uriencoding= "GBK". The modified connector is set to:
java code <connector port= "8080" maxhttpheadersize= "8192" maxthreads= "minsparethreads=" maxsparethreads= "75" enablelookups= "false" Redirectport= "8443" acceptcount= "+" connectiontimeout= "20000" disableuploadtimeout= "true" <span style= "COLOR: #ff0000" > uriencoding= "GBK" </SPAN>/> <connector port= "8080" maxhttpheadersize= "8192" maxthreads= "minsparethreads=" maxsparethreads= "75" Enablelookups= "false" redirectport= "8443" acceptcount= "100" connectiontimeout= "20000" disableuploadtimeout= "true" uriencoding= "GBK"/> * Note that restarting Tomcat after modification will work. Use of/*********************************************************** filter ********************************************* **********/ 3, the form get submit way of garbled processing way. If you use get to submit the Chinese language, the page that accepts parameters will also appear garbled, this garbled reason is also tomcat internal encoding format iso8859-1 caused. Tomcat will encode the kanji with the default encoding of GET, append to the URL after encoding, and result in the iso8859-1 of the parameters on the receiving page. Workaround: A, using the first method in the above example, the accepted characters are decoded and then transcoded. B, get Go is URL commit, and before entering the URL has been iso8859-1 encoding processing. To affect this encoding you need to add usebodyencodingforuri= "true" to the connector node of the Server.xml Property configuration, you can control how Tomcat Chinese character coding The Get method, which controls that the get commits are encoded using the encoding format set by request.setcharacterencoding ("UTF-8"). So automatically encoded as utf-8, accept the page to accept the normal.
But I think the real coding process is that Tomcat is also based on <connector port= "8080" maxthreads= "minsparethreads=" maxsparethreads= "75" Enablelookups= "false" redirectport= "8443" acceptcount= "100" debug= "0" connectiontimeout= "20000" usebodyencodingforuri= "true" Disableuploadtimeout= "true" uriencoding= "UTF-8"/> The uriencoding= "UTF-8", which is set inside, is encoded again, but the encoding is not changed because it is encoded as utf-8. If the encoding is obtained from the URL, the Accept page is decoded according to uriencoding= "UTF-8". 4, garbled when uploading a file when uploading a file, the form form is set to Enctype= "Multipart/form-data". This way, the file is submitted in a streaming manner. If you use the Apach upload component, you will find a lot of garbled imagination. This is because the Apach of the early Commons-fileupload.jar has a bug, take out the Chinese characters after decoding, because this way to commit, encoding and automatically use the Tomcat default encoding format iso-8859-1. But the garbled problem is: period, comma, and other special symbols become garbled, if the number of Chinese characters is odd, it will appear garbled, even the analytic normal. Workaround: Download Commons-fileupload-1.1.1.jar This version of the jar has been resolved by these bugs. However, when you remove the content, you still need to transcode the extracted characters from iso8859-1 to Utf-8. have been able to get all the characters as well as normal. 5,java code about URL request, accept parameter garbled The encoding format of the URL depends on the uriencoding= "UTF-8" described above. If this encoding format is set, it means that all Chinese character parameters to the URL must be encoded. Otherwise the obtained Chinese character parameter value is garbled, for example A link response.sendderect ("/a.jsp?name= Zhang Dawi"), and in a.jsp directly use String name = Request.getparameter ("name"); get is garbled. Because the rules must be utf-8, so this turn should be written like this: Response.sendderect ("/a.jsp?name=urlencode.encode" ("Zhang Dawi", "utf-8"); What happens if you do not set this parameter uriencoding= "UTF-8"? If not set, the default encoding format iso8859-1 is used. The problem comes out again, the first is the number of parameter values if it is an odd number of numbers, it can be parsed normally, if you make an even number of numbers, get the last character is garbled. There is also if the last character if it is in English, it will be able to parse normally, but Chinese punctuation is still garbled. Expedient, if your parameters do not have Chinese punctuation, you can add an English symbol at the end of the parameter value to solve the garbled problem, get the parameters and then remove the most behind the symbol. can also be pooled or used.
6, script code about URL request, accepted parameter garbled The script also controls the page steering, as well as the accompanying parameters and the case where the page is parsed for the parameter. If this Chinese character parameter does not carry on the uriencoding= "UTF-8" the encoding processing, then accepts the page to accept the Chinese character also garbled. The script processing code is troublesome, must have the corresponding encoding script corresponding file, then calls the script the method to encode the Chinese character.
7, the garbled problem of JSP opening in MyEclipse For an already existing project, the storage format of the JSP file may be utf-8. If the new eclipse is installed, the encoding format used by default is iso8859-1. So the JSP inside the Chinese characters appear garbled. This garbled is easier to solve, go directly to eclipse3.1 preferences inside find General-〉edidor, set to your file open code for Utf-8 can. Eclipse will automatically re-open in the new encoded format. Chinese characters can be displayed normally.
8, about HTML page opened in Eclipse garbled situation Since most of the pages are made by Dreamweaver, their storage format differs from Eclipse's recognition. In this case, create a new JSP in Eclipse and paste it directly from the Dreamweaver copy page content to the JSP
But I think the real coding process is that Tomcat is also based on <connector port= "8080" maxthreads= "minsparethreads=" maxsparethreads= "75" Enablelookups= "false" redirectport= "8443" acceptcount= "100" debug= "0" connectiontimeout= "20000" usebodyencodingforuri= "true" Disableuploadtimeout= "true" uriencoding= "UTF-8"/> The uriencoding= "UTF-8", which is set inside, is encoded again, but the encoding is not changed because it is encoded as utf-8. If the encoding is obtained from the URL, the Accept page is decoded according to uriencoding= "UTF-8". 4, garbled when uploading a file when uploading a file, the form form is set to Enctype= "Multipart/form-data". This way, the file is submitted in a streaming manner. If you use the Apach upload component, you will find a lot of garbled imagination. This is because the Apach of the early Commons-fileupload.jar has a bug, take out the Chinese characters after decoding, because this way to commit, encoding and automatically use the Tomcat default encoding format iso-8859-1. But the garbled problem is: period, comma, and other special symbols become garbled, if the number of Chinese characters is odd, it will appear garbled, even the analytic normal. Workaround: Download Commons-fileupload-1.1.1.jar This version of the jar has been resolved by these bugs. However, when you remove the content, you still need to transcode the extracted characters from iso8859-1 to Utf-8. have been able to get all the characters as well as normal. 5,java code about URL request, accept parameter garbled The encoding format of the URL depends on the uriencoding= "UTF-8" described above. If this encoding format is set, it means that all Chinese character parameters to the URL must be encoded. Otherwise the obtained Chinese character parameter value is garbled, for example A link response.sendderect ("/a.jsp?name= Zhang Dawi"), and in a.jsp directly use String name = Request.getparameter ("name"); get is garbled. Because the rules must be utf-8, so this turn should be written like this: Response.sendderect ("/a.jsp?name=urlencode.encode" ("Zhang Dawi", "utf-8"); What happens if you do not set this parameter uriencoding= "UTF-8"? If not set, the default encoding format iso8859-1 is used. The problem comes out again, the first is the number of parameter values if it is an odd number of numbers, it can be parsed normally, if you make an even number of numbers, get the last character is garbled. There is also if the last character if it is in English, it will be able to parse normally, but Chinese punctuation is still garbled. Expedient, if your parameters do not have Chinese punctuation, you can add an English symbol at the end of the parameter value to solve the garbled problem, get the parameters and then remove the most behind the symbol. can also be pooled or used.
6, script code about URL request, accepted parameter garbled The script also controls the page steering, as well as the accompanying parameters and the case where the page is parsed for the parameter. If this Chinese character parameter does not carry on the uriencoding= "UTF-8" the encoding processing, then accepts the page to accept the Chinese character also garbled. The script processing code is troublesome, must have the corresponding encoding script corresponding file, then calls the script the method to encode the Chinese character.
7, the garbled problem of JSP opening in MyEclipse For an already existing project, the storage format of the JSP file may be utf-8. If the new eclipse is installed, the encoding format used by default is iso8859-1. So the JSP inside the Chinese characters appear garbled. This garbled is easier to solve, go directly to eclipse3.1 preferences inside find General-〉edidor, set to your file open code for Utf-8 can. Eclipse will automatically re-open in the new encoded format. Chinese characters can be displayed normally.
8, about HTML page opened in Eclipse garbled situation Since most of the pages are made by Dreamweaver, their storage format differs from Eclipse's recognition. In this case, create a new JSP in Eclipse and paste it directly from the Dreamweaver copy page content to the JSP
2008-02-11 11:54 In the process of using JSP, one of the most troublesome problem is Chinese garbled problem, the following is my software development encountered garbled problems and solutions.
1, JSP page garbled The reason for this garbled is that it should not be specified in the page character set encoding, the workaround: As long as the page at the beginning of the code to specify the character set encoding, <%@ page contenttype= "text/html; Charset=utf-8 "%> 2, Database garbled This garbled character will make you insert the database of Chinese into garbled, or read out the display is also garbled, the solution is as follows: To include the coded character set in the database connection string String url= "jdbc:mysql://localhost/digitgulf?user=root&password=root&useunicode=true& Characterencoding=utf-8 "; and use the following code in the page: Response.setcontenttype ("Text/html;charset=utf-8"); Request.setcharacterencoding ("Utf-8"); 3, Chinese as a parameter to pass garbled When we pass a Chinese character as an argument to another page, there will also be garbled cases, the solution is as follows: The parameter is encoded when the parameter is passed, such as Rearshres.jsp?keywords= "+ java.net.URLEncoder.encode (keywords) Then, on the Receive Parameters page, use the following statement to receive Keywords=new String (request.getparameter ("keywords"). GetBytes ("8859_1")); Above for the current situation encountered garbled problem, garbled core problem or character set encoding problem, as long as mastered this point, the general garbled problem can be solved. |