- Introduction
- Chinese garbled characters often occur during Java project development, which is a problem that has always plagued everyone. Many programmers often encounter this problem.
- Sometimes you can't figure out the cause of this mess for a long time without knowing what went wrong.
- The following is a brief introduction of common Java Chinese Garbled text and corresponding solutions. This will allow us to find out how to handle Garbled text in future development.
- J2EE Program Analysis
- Anyone who is engaged in Java knows that Java projects are managed by container management (Tomcat, all types of files in the project, such as JSP, Servlet, Java, and class initialization, object creation, collection, compilation, and running, are centrally managed by containers. Therefore, in Java, containers can be used to set the character set encoding mode in a unified manner, so that Java encoding can be unified, and garbled code is often not internal, but in the process of interacting and dealing with the outside world, Java garbled characters are produced by different internal and external character sets. What external interactions does Tomcat mainly interact?
- As mentioned above, you can change the Java encoding by setting the container encoding mode.
How to encode the code before it is deployed in the container. As long as the encoding problem is involved in its various operations, it will be automatically converted to unicode encoding, and the default encoding of the operating system is used before it is converted to Unicode, this is also a reason why Java can be cross-platform.
- Directly interacting with Java is usually the browser, database, etc. JAVA input and output are also dealing with them. The unified Input and Output encoding methods of the Java System are the principle to solve the garbled problem.
- Causes of Chinese garbled characters and Solutions
- Java and JSP file Compilation
- First, Java (including JSP) source files may contain Chinese characters, while Java and JSP source files are stored Based on byte streams. If Java and JSP are compiled into class files, if the encoding method used is inconsistent with the source file encoding, garbled characters may occur.
- Solution (commonly used)
-
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
- JAVA interaction with other software
- When data is transmitted in Java in the same browser, database, file, page, and so on, conversion between bytes and characters is required. Conversion between Java characters and other bytes is required, if the encoding method used in the above conversion process is inconsistent with the original Byte encoding, garbled characters may occur.
- Garbled characters between JSP and page Parameters
- The default encoding method is generally used to obtain page parameters. If the encoding type of the page parameters is different from the default encoding type, garbled characters may occur. The basic method to solve this type of Garbled text problem is to forcibly specify the encoding method of the request parameters before obtaining the parameters on the page:
-
request.setCharacterEncoding("GB18030")
- If garbled characters appear when JSP outputs variables to the page, you can set
- Code
response.setContentType("text/html;charset=GBK")
- If we have too many JSP files and it is difficult to set one by one, we can set them in the configuration file as follows:
-
<! -- Filter configuration --> <filter-Name> charsetencodingfilter </filter-Name> <filter-class> COM. bjpowernode. DRP. util. filter. charsetencodingfilter </filter-class> <init-param> <param-Name> encoding </param-Name> <param-value> gb18030 </param-value> </init- param> </filter> <filter-mapping> <filter-Name> charsetencodingfilter </filter-Name> <URL-pattern> *. JSP </url-pattern> </filter-mapping> <filter-Name> charsetencodingfilter </filter-Name> <URL-pattern>/servlet/* </url-pattern> </filter-mapping>
3. In DRP, garbled characters occur during parameter passing from servlet to JSP. garbled characters still exist after encoding using urlencoder. encode (). The Code is as follows:
A. Use response. sendredirect () in servlet for redirection
// Redirect to the home page response. sendredirect (request. getcontextpath () + "/basedata/item_maint.jsp?
errorMessage="+java.net.URLEncoder.encode(message,"GB18030"));
B. parameters accepted in JSP
Convert character to byte
new String(request.getParameter("errorMessage").getBytes("8859_1"),"GB18030")
- Garbled characters between Java and databases
- Most databases support Unicode encoding, so it is wise to solve the garbled problem between Java and databases by directly using Unicode encoding to interact with the database.
- Garbled characters between Java and files/streams
- The most common classes for reading and writing files in Java are fileinputstream/fileoutputstream and filereader/filewriter. Fileinputstream and fileoutputstream are based on byte streams and are often used to read and write binary files. We recommend that you use character-based filereader and filewriter to read and write character files, eliminating the need for conversion between bytes and characters. However, the constructors of these two classes use the system encoding method by default. If the file content is inconsistent with the system encoding method, garbled characters may occur.
- Inputstreamreader/outputstreamwriter, which are character-based, but the encoding type can be specified in the constructor.
-
Inputstreamreader (inputstream in, charset CS) and outputstreamwriter (outputstream out, charset CS ).
IIV. Summary
- Only by knowing the cause of the problem can we quickly find a solution. When the problem occurs, we do not simply look for how to do it from the Internet, and then perform simple settings according to the steps described by others, let's fix the problem. If you don't know the cause, you won't do the same thing the next time you see it, just do what others say, imitating is always something of others. If you want to become your own, you must do it yourself.