Article turned from: http://blog.csdn.net/beijiguangyong/article/details/7414247
Before the introduction of the method, we should first understand the specific issues, the author of this blog in the JSP Chinese garbled problem has the following aspects: page garbled, parameter garbled, form garbled, source file garbled. The following to solve the garbled problem.
One, JSP page Chinese garbled
In the JSP page, the Chinese display garbled there are two cases: one is the HTML of the 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 displaying a few words in Chinese on the page and the title is Chinese. Display in the browser after running
The reason is that there is no encoding to specify the page display in the JSP, the solution to eliminate garbled is very simple the page command in the code above is modified to resemble the following
<%@ page language= "java" import= "java.util.*" contenttype= "text/html"; charset=gb2312 "%>
Again run garbled disappears, the principle is to specify the page encoding as GB2312, then the page will follow this code to display, so garbled disappears.
Second, url pass parameter Chinese garbled
In general, when you submit a form using the Get method, the arguments passed 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 the above JSP program is to pass a URL link to itself, the parameter is a Chinese string, the program's operation effect as
For the URL to pass the Chinese parameter garbled this problem, its processing method is very special, just convert the Chinese string or set JSP page display encoding is not solve the problem, you need to modify the Tomcat server configuration file to solve the problem. Here to modify the server.xml configuration file in Tomcat's Conf directory, the following code is changed
<connector port= "8080" protocol= "http/1.1" uriencoding= "gb2312" connectiontimeout= " 20000" redirectport= "8443"/>
Third, the form submitted in Chinese garbled
The data for the form can be obtained using the Request.getparameter ("") method, but garbled when Chinese data is present in the form.
The sample code is as follows
<%@ page language= "java" import= "java.util.*" contenttype= "text/html;charset=gb2312"%>
In the above form want to acceptformcharset this page to raise two data, 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 is not Chinese, it can be normal to display when the input data in Chinese, the results obtained.
The reason for this is that the default encoding for the forms 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 used in the conversion encoding format method to solve, now acceptfromcharset this page changes as follows:
<%@ page language= "java" import= "java.util.*" contenttype= "text/html;charset=gb2312"%>
After this conversion code, all Chinese input can be extracted with the request object normally. In this program, lines fourth and fifth are the key to converting the encoding format, taking the byte contents out of the Iso-8859-1-formatted string, and then re-constructing a new string in the encoded format of the GB2312. This allows the normal value and display of the Chinese dimmed input to be supported. After the improvement, the results of the program are as follows
After the above changes in the encoding format of 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 and converted, which can be cumbersome. This is where we use the famous filter filters. The general thinking about the content here is the same as the above. Please refer to the author's other article.
"Turn" JSP Chinese garbled problem the ultimate solution