"content-type" content="text/html; charset=gb2312" /> <title>title</title>
The meta section is used to specify the encoding format of the current HTML, note that this paragraph should be placed in the head tag and placed at the front of the head tag, if not the front of the first IE may cause problems, especially in the title of the case of Chinese.
After completing the above three-paragraph test, we can guarantee that the output JSP page will display Chinese correctly.
2. Post garbled
First add the form to the method= "post", let the form submitted by using the Post method.
When sending a request, the encoding used is iso-8859-1, which means that only English is a valid character, this restriction is because the members of the HTTP standard originally specified are from English-speaking countries, so if you use the default way to obtain data from the request, Chinese will all become garbled.
If you do not believe, you can enter Chinese in the example just now, then submit:
The results of the submission will look like this:
How to solve it? We want to prefix the JSP with a Java statement that sets the character encoding of the request.<% request.setcharacterencoding ("gb2312"); %>
As a result, those garbled characters are normal:
3. Get garbledWarningGet case, using UrlEncode () does solve garbled problems, which needs to be supplemented.
By clicking the hyperlink directly, the default submission method for form is get. The solution in post mode is simple, because the data submitted in post mode is appended to the body portion of the HTTP request in binary mode, and it is only sufficient to specify the encoding format in the background. The Get method appends parameters directly to the URL, which cannot be processed using request.setcharacterencoding (), and the result is that all Chinese in the Get form is garbled.
There is no easy way to do this, only one of these Chinese can be converted, using the new String (bytes, "gb2312") for transcoding.There is no easy way to do this, only one of these Chinese can be converted, using the new String (bytes, "gb2312") for transcoding.
<% String username = request.getparameter ("username"); byte[] bytes = username.getbytes ("iso-8859-1"); string result = new String (bytes, "gb2312"); Out.print (result); %>As we can see, we first get the parameters from request, then break the string into byte array according to ISO-8859-1 encoding, then use the GB2312 encoding group to synthesize the new string, and finally print out the normal Chinese.
Written together, it becomes:
<%=new string (New String (Request.getparameter ("username"). GetBytes ("Iso-8859-1"), "gb2312")%>
The disadvantage of this is that all the Chinese that are obtained from the request need to be transcoded, very cumbersome.
This solves the Chinese garbled question is too cumbersome, does the general test example also can, does the large-scale project to be very troublesome, therefore generally uses the filter to solve. Under the spring framework. Example 1
<!--Spring's character filter, not required, can be resolved in other ways-<filter> <FILTER-NAME>CHARACTERENCODINGFILTER&L t;/filter-name> <filter-class > Org.springframework.web.filter.characterencodingfilter</filter-class > <init-param> <param-name>encoding</param-name> <param-value>utf- 8 </param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true & lt;/param-value> </init-param> </filter> <filter-mapping> <filter-name> ; characterencodingfilter</filter-name> <url-pattern>/* </url-pattern> </filter-mapping>
Example 2 We can customize a filter to solve the problem of Chinese garbled
1 Importjava.io.IOException;2 ImportJavax.servlet.Filter;3 ImportJavax.servlet.FilterChain;4 ImportJavax.servlet.FilterConfig;5 Importjavax.servlet.ServletException;6 Importjavax.servlet.ServletRequest;7 ImportJavax.servlet.ServletResponse;8 9 Public classEncodingfilterImplementsFilter {Ten One Public voidInit (filterconfig config)throwsservletexception {} A - Public voiddestroy () {} - the Public voidDoFilter (servletrequest request, - servletresponse Response, - filterchain chain) - throwsIOException, servletexception { + -Request.setcharacterencoding ("gb2312"); + Chain.dofilter (request, response); A } at -}View CodeThe filter interface is implemented in this encodingfilter, and the three methods defined in the filter interface are implemented in Encodingfilter, where Dofilter () Code to implement the main function: Set GB2312 encoding for the request and perform chain.dofilter () to continue with the following operation.
Similar to a servlet, it needs to be configured in Web. XML in order for the filter to work.
<filter> <filter-name>EncodingFilter</filter-name> <filter-class>anni. Encodingfilter</filter-class></filter><filter-mapping> <filter-name>encodingfilter </filter-name> <url-pattern>/*</url-pattern></filter-mapping>
The filter label section defines the filters used, and the filter-mapping tag tells the server which requests to be processed by the filter. The/* Here indicates all requests,/represents the root path, * (asterisk) represents all requests, and together it becomes all requests under the root path.
In this way, all requests are intercepted by Encodingfilter and the specified gb2312 encoding is set on the request.