Share JSP Chinese garbled solution _jsp programming

Source: Internet
Author: User
Tags tomcat tomcat server

One, JSP page Chinese garbled

In the JSP page, the Chinese display garbled there are two kinds of situation: one is HTML in Chinese garbled, the other is in the JSP dynamic output of Chinese garbled.

First look at a JSP program:

<%@ page language= "java" import= "java.util.*"%> 
 
 

The above JSP program looks like it is showing a few words in Chinese on the page and the title is Chinese. After running, it appears in the browser as shown in the picture

The reason is that there is no code in the JSP to specify the page display, the elimination of garbled solution is very simple the page command in the code above can be modified as follows

<%@ page language= "java" import= "java.util.*" contenttype= "text/html; charset=gb2312 "%> 
 
 

Again run garbled disappear, the principle is to specify the code to the page for GB2312, then the page will follow this code to display, so garbled disappeared.

Second, URL transfer parameters Chinese garbled

In general, the parameters passed when you submit a form using the Get method are likely to be garbled if they are in Chinese.

Here is an example program

<%@ page language= "java" import= "java.util.*" contenttype= "text/html;charset=gb2312"%> 
 
 

The function of this JSP program is to pass a parameter to itself through a URL link, this parameter is the Chinese string, the operation effect of this program is as follows

For the URL to pass the Chinese parameter garbled this problem, its processing method is very special, just convert this Chinese string or set JSP page display code is not solve the problem, need to modify the Tomcat server configuration file to solve the problem. Here to modify the tomcat in the Conf directory of the Server.xml configuration file, the specific changes to the following code

<connector port= "8080" protocol= "http/1.1" uriencoding= "gb2312" 
        connectiontimeout= "20000"  
        

Add the URI encoding to the original code setting uriencoding= "gb2312" to restart the Tomcat server to get the correct page. The principle is similar to the above, that is, the code type is indicated to the program, and then the display is normal.

Third, the form submits the Chinese garbled

The data for the form can be obtained using the Request.getparameter ("") method, but garbled characters appear when the Chinese data appears in the form.

The sample code is as follows

<%@ page language= "java" import= "java.util.*" contenttype= "text/html;charset=gb2312"%> 
 
 

In the above form to acceptformcharset this page to increase the price of two data, the following is acceptformcharset.jsp content:

<%@ page language= "java" import= "java.util.*" 
  contenttype= "text/html;charset=gb2312"%> 
 
 

In the above program, if the form input does not have Chinese, it is normal to display when the input data has Chinese, the results are as shown in the figure.

The reason for this is that the default encoding for the form submitted by the Post method in Tomcat is iso-8859-1, and the encoding format does not support Chinese characters. For this problem can be converted into a coded format to solve, now the Acceptfromcharset this page changes are as follows:

<%@ page language= "java" import= "java.util.*" 
  contenttype= "text/html;charset=gb2312"%> 
 
 

After such conversion coding, all Chinese input can be taken out normally with the request object. In the above program, lines fourth and fifth are key to converting the encoding format, removing the byte content from the Iso-8859-1 format string, and then constructing a new string in the GB2312 encoded format. This will support the normal values and display of the Chinese fading input. After the improvement, the results of program operation are as follows

After the above changes in the encoding format processing, the form of Chinese input garbled problem has been resolved. But if there are more than two entries in the form above, then each entry needs to be encoded, which is cumbersome. This is where we used the famous filter filters. About the contents of the general thinking and the same as above, please refer to the author of another article.

Four, eclipse JSP files in Chinese garbled

In eclipse or myeclipse because the default JSP encoding format is iso-8859-1, garbled text appears when you open a JSP file that is edited by another editor, as shown in the figure

All we need to do is change eclipse or the default encoding for JSP in MyEclipse, and the modified place (my MyEclipse version is 11) as shown

Ps

In Eclipse or MyEclipse, the default encoding for JSP files is iso-8859-1, so it cannot be saved in the middle of JSP code, such as the following code

<%@ page language= "java" import= "java.util.*"%> 
 
 

After the change, you will be prompted as follows:

The reason for this is that the JSP source file has iso=8859-1 encoding unrecognized Chinese characters, for this problem, the solution is to declare the page in the JSP page encoding format can be. After the declaration the Code is as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "GB2312"%> 
 
 

The first line in the pageencoding= "gb2312" indicates that the JSP page encoding using GB2312, so that the JSP can normally save the source file.

The above is the full content of this article, I hope you can enjoy.

Please take a moment to share the article with your friends or leave a comment. We will sincerely thank you for your support!

Related Article

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.