The ultimate solution for JSP Chinese garbled problem, and the ultimate solution for jsp
Reprinted from: http://www.cnblogs.com/beijiguangyong/
Http://www.cnblogs.com/beijiguangyong/archive/2012/03/31/2437124.html
Before introducing the methods, we should first understand the specific problems. The JSP Chinese Garbled text discussed in this blog has the following aspects: page garbled, parameter garbled, form garbled, source file garbled. Next we will solve the garbled problem one by one.
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:
[Java]View plaincopyprint?
- <% @ Page language = "java" import = "java. util. *" %>
- <Html>
- <Head>
- <Title> Chinese display example </title>
- </Head>
- <Body>
- This is an example of Chinese display:
- <%
- String str = "Chinese ";
- Out. print (str );
- %>
- </Body>
- </Html>
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:
[Java]View plaincopyprint?
- <% @ Page language = "java" import = "java. util. *" contentType = "text/html; charset = GB2312" %>
- <Html>
- <Head>
- <Title> Chinese display example </title>
- </Head>
- <Body>
- This is an example of Chinese display:
- <%
- String str = "Chinese ";
- Out. print (str );
- %>
- </Body>
- </Html>
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
[Java]View plaincopyprint?
- <% @ Page language = "java" import = "java. util. *" contentType = "text/html; charset = gb2312" %>
- <Html>
- <Head>
- <Title> Chinese processing example of URL Transfer Parameters </title>
- </Head>
- <%
- String param = request. getParameter ("param ");
- %>
- <Body>
- <A href = "URLCharset. jsp? Param = 'Chinese' "> click this link </a> <br>
- The parameter you submitted is: <% = param %>
- </Body>
- </Html>
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:
[Html]View plaincopyprint?
- <Connector port = "8080" protocol = "HTTP/1.1" URIEncoding = "gb2312"
- ConnectionTimeout = "20000"
- RedirectPort = "8443" type = "regxph" text = "yourobjectname"/>
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:
[Java]View plaincopyprint?
- <% @ Page language = "java" import = "java. util. *" contentType = "text/html; charset = gb2312" %>
- <Html>
- <Head>
- <Title> Form Chinese processing example </title>
- </Head>
- <Body>
- <Font size = "2">
- The following is the form content:
- <Form action = "AcceptFormCharset. jsp" method = "post">
- UserName: <input type = "text" name = "userName" size = "10"/>
- Password: <input type = "password" name = "password" size = "10"/>
- <Input type = "submit" value = "submit">
- </Form>
- </Font>
- </Body>
- </Html>
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:
[Java]View plaincopyprint?
- <% @ Page language = "java" import = "java. util .*"
- ContentType = "text/html; charset = gb2312" %>
- <Html>
- <Head>
- <Title> Form Chinese garbled characters </title>
- </Head>
- <Body>
- <Font size = "2"> The following is the form data obtained by the request after the form is submitted: <br>
- <%
- String userName = request. getParameter ("userName ");
- String password = request. getParameter ("password ");
- Out. println ("form input userName value:" + userName + "<br> ");
- Out. println ("input password in the form:" + password + "<br> ");
- %>
- </Font>
- </Body>
- </Html>
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:
[Java]View plaincopyprint?
- <% @ Page language = "java" import = "java. util .*"
- ContentType = "text/html; charset = gb2312" %>
- <Html>
- <Head>
- <Title> Form Chinese garbled characters </title>
- </Head>
- <Body>
- <Font size = "2"> The following is the form data obtained by the request after the form is submitted: <br>
- <%
- String userName = request. getParameter ("userName ");
- String password = request. getParameter ("password ");
- Out. println ("form input userName value:" + new String (userName. getBytes ("ISO-8859-1"), "gb2312") + "<br> ");
- Out. println ("form input password value:" + new String (password. getBytes ("ISO-8859-1"), "gb2312") + "<br> ");
- %>
- </Font>
- </Body>
- </Html>
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
[Java]View plaincopyprint?
- <% @ Page language = "java" import = "java. util. *" %>
- <Html>
- <Head>
- <Title> Chinese display example </title>
- </Head>
- <Body>
- This is an example of Chinese display:
- <%
- String str = "Chinese ";
- Out. print (str );
- %>
- </Body>
- </Html>
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:
[Java]View plaincopyprint?
- <% @ Page language = "java" import = "java. util. *" pageEncoding = "GB2312" %>
- <Html>
- <Head>
- <Title> Chinese display example </title>
- </Head>
- <Body>
- This is an example of Chinese display:
- <%
- String str = "Chinese ";
- Out. print (str );
- %>
- </Body>
- </Html>
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.
When you encounter a problem, first analyze the cause of the problem. You can solve the problem only by knowing the cause. Learning and analyzing the source of the problem is far more important than solving the problem.
The cause of the garbled code problem is that the encoding of the Program (Eclipse or browser) is not consistent with that of the programmer, (just like you can communicate with a person who doesn't understand Chinese. Of course, he doesn't understand.) to solve this problem, you only need to tell the program the code that the programmer wants, the above methods to solve the garbled problem can be said to be a process of declaring code, that is, the ultimate solution to the garbled problem is:Transcoding.The transcoding here is either manually transferred by the programmer or declared to let the program go, that is, to communicate with people who do not understand Chinese, or to learn Chinese, you can either learn his language.
Life is programming, and programming is life !!!