Share JSP Chinese garbled code solutions and jsp garbled code

Source: Internet
Author: User

Share JSP Chinese garbled code solutions and jsp garbled code

I. garbled Chinese characters on JSP pages

On the JSP page, there are two types of Chinese garbled characters: Chinese garbled characters in HTML and Chinese garbled characters dynamically output in JSP.

First look at a JSP program:

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

The above JSP program seems to display a few Chinese sentences on the page and the title is also Chinese. Display in the browser after running

The reason is that the Code displayed on the page is not specified in JSP. The solution to eliminating Garbled text is simple. In the above Code, the page command is changed to the following:

<% @ Page language = "java" import = "java. util. * "contentType =" text/html; charset = GB2312 "%> 

Run Garbled text again. The principle is to specify the encoding to GB2312 for the page. The page will be displayed according to the encoding, so the Garbled text disappears.

Ii. garbled Chinese characters of URL passing Parameters

In general, garbled characters may occur if the parameters passed when the get method is used to submit a form.

The following is an example Program

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

The above JSP program function is to pass a parameter to itself through a URL link. this parameter is a Chinese string, and the running effect of this program is as follows:

The Processing Method for garbled Chinese parameters transmitted by URLs is special. The problem cannot be solved only by converting the Chinese string or setting the display encoding of the JSP page, you must modify the configuration file of the Tomcat server to solve the problem. Modify the server. xml configuration file in the conf directory of Tomcat. The code after modification is as follows:

<Connector port="8080" protocol="HTTP/1.1" URIEncoding="gb2312"         connectionTimeout="20000"          redirectPort="8443" /> 

Add URI encoding in the original code and set URIEncoding = "gb2312". Restart the Tomcat server to get the correct page. The principle is similar to the above situation, that is, to specify the encoding type for the program, and then the display is normal.

3. garbled Chinese Characters in Form submission

You can use the request. getParameter ("") method to obtain the data of the form. However, garbled characters may appear when the Chinese data in the form is displayed.

The sample code is as follows:

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

In the above form, we want to increase the price of two items on the AcceptFormCharset page. The following is the content of AcceptFormCharset. jsp:

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

In the above program, if the form input does not contain Chinese characters, the results will be displayed normally when the input data contains Chinese characters.

The reason for this is that the form submitted by the post method in Tomcat uses the default encoding for the ISO-8859-1, and this encoding format does not support Chinese characters. We can solve this problem by converting the encoding format. The changes to the AcceptFromCharset page are as follows:

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

After such conversion and encoding, all Chinese input can be taken out normally using the request object. In the above program, the fourth line and the fifth line are the key to the conversion of the encoding format, first from the ISO-8859-1 format string to retrieve the byte content, then, reconstructs a new string in GB2312 encoding format. In this way, the normal value and display of Chinese fade-In are supported. After improvement, the program runs as follows:

After the above encoding format change, the Chinese Input garbled characters in the form have been resolved. However, if there are more than two input items in the form above, it is very troublesome to encode and convert each input item. This is a well-known filter. For more information about the content, see the author's article.

4. garbled Chinese Characters in JSP files in Eclipse

In Eclipse or MyEclipse, because the default JSP encoding format is ISO-8859-1, garbled characters appear when opening JSP files edited by other editors,

To solve this problem, you only need to change the default code of JSP in Eclipse or MyEclipse. The modification is made (My MyEclipse version is 11)

PS

In Eclipse or MyEclipse, JSP files are encoded as ISO-8859-1 by default, so if there is a Chinese character in the JSP code, it cannot be saved, such as the following code

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

After the modification, the following message is displayed when you save the modification:

The cause of this prompt is that the JSP source file contains Chinese characters that are not recognized by ISO = 8859-1 encoding. To solve this problem, declare the page encoding format on the JSP page. The declared code is as follows:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" GB2312 "%> 

In the first line, pageEncoding = "gb2312" indicates that the JSP page encoding adopts GB2312, so that the source file of JSP can be properly saved.

The above is all the content described in this article. I hope you will like it.

Please take a moment to share your article with your friends or leave a comment. 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.