The garbled problem in Javaweb programming

Source: Internet
Author: User

The garbled problem in the program appears mainly in the process of processing Chinese data. Requests data from the browser to the server, and the data returned by the server is displayed in the browser as garbled. Or the Java files in the server to use in Chinese, there may be garbled. When processing data, the database will also encounter garbled problems.

Garbled problem in general, is the encoding method is caused by different, in the establishment of data, transmission, processing and display process, if the processing of data encoding different, it will be easy to cause Chinese garbled problem. The solution to the macro-scale, is in the data processing in all aspects of the use of a unified coding, so you can avoid the occurrence of garbled problems.

For Javaweb programming, it mainly involves the parts of HTML, XML, database, Java files and Java code. We use the "UTF-8" code as an example to say some of the settings in different files or the processing of code.

First in the HTML file, is to tell the browser this HTML file encoding method is UTF-8. The code should be placed in the head of the header file,

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">

This means that the data inside the file is encoded with UTF-8, the data browser in it will be opened in accordance with UTF-8 encoding processing, if the data sent to the server, the server should also be opened with UTF-8 encoding.

Second, in XML, the first sentence of the generic XML file is set to encode. The code is:

<?xml version= "1.0" encoding= "UTF-8"?>

The third is in the database, to the MySQL database, for example, when the database is established, will choose the encoding method, it is necessary to choose UTF-8, continue to establish the table, the field can be the default encoding, that is, and database consistency, This ensures that all data in this database is UTF-8 encoded.

The four is Java files, to ensure that the Java file encoding is UTF-8, generally using the Java programming tool settings, such as MyEclipse software, is through windows---->preferences, navigation found general---- >workspace, right window text file encoding, select Other, change to UTF-8, and later create the project its Properties dialog box, text file encoding is UTF-8. Click "OK" to save. Of course, there are other ways to set up, and here are just a few of them. For other Java programming tools, you can find the corresponding encoding setting method to operate.

Five is the code in the Java file, especially when Javaweb programming, from the browser to obtain data, processing and return to the browser display, this time need to do some processing. For example, our development tool is MyEclipse, the server with its own tomcat, because the Tomcat default encoding is iso-8859-1, so it is easy to create garbled, the process is such. First, convert the string data from the browser to a binary data, and then revert to a string using UTF-8 encoding. The code is:

Byte[] Bytes=txt.getbytes ("iso-8859-1");

String utf8txt=new string (bytes, "UTF-8");

Before returning the UTF-8 encoded string utf8txt to the browser, you need to specify the encoding again in the Java code. The code is:

Resp.setcontenttype ("Text/html,charset=utf-8");

Resp.setcharacterencoding ("UTF-8");

And then continue to display this data in HTML, the code is:

Resp.getwriter (). Print ("

Resp.getwriter (). Print ("<strong>" +utf8txt+ "</strong>");

Resp.getwriter (). Print ("</body>

This will ensure that the browser sees the data that is showing normal.

For Javaweb programming encountered in the garbled problem generally these can be solved. If in the programming encountered garbled problem, we can step-by-step debugging, find out the location of garbled occurrences, and then targeted resolution. In the Java file to set breakpoints, see from the browser to get the data is normal, from the database to get the data is normal, from the Tomcat converted data is normal, until the server returned to the browser data is normal this idea to step by step analysis, I believe garbled problem will be quickly found, and find a solution.

The above analysis is from such as Peng Nets Yang Zhengko teacher to solve garbled problem get inspiration, do a small summary, hope to encounter garbled problem friends help.

The garbled problem in Javaweb programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.