First, the server side received the browser-side transfer of data garbled problem!!! (garbled when fetching data from servlet side)
1. Why is there a garbled problem with the form submission in Chinese? (Non-Ajax mode)
Browser-side: When the form is submitted, the browser encodes the Chinese parameter values, which are encoded using the character set used when the page that the form is opened in (that is, using the <%@ page contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%> the specified character set for encoding)
Server-side: The servlet is decoded by default using Iso-8859-1.
Therefore: the browser side and the server-side encoding and decoding inconsistent, and iso-8859-1 itself does not support the Chinese encoding and decoding, resulting in garbled characters.
2. How to solve the garbled problem of the servlet side? (We recommend using scenario Two + solution three to solve the problem of garbled)
The first scenario: The garbled string is restored to byte as Iso-8859-1, and then the restored byte is encoded as a string in the UTF-8 way.
Examples are as follows: byte[] Bs=str.getbytes ("iso8859-1");
Str=new String (BS, "UTF-8");
Pros: Both get and POST request methods apply
Cons: More Trouble
Second Scenario: Modify the Tomcat profile Server.xml, add the uriencoding attribute in the Connector tab of the Server.xml, and set the uriencoding= "Utf-8"
Examples are as follows: <connector connectiontimeout= "20000" port= "8085" protocol= "http/1.1" redirectport= " 8443" uriencoding= "Utf-8"/>
Advantages: Simple
Cons: Valid only for GET requests, invalid for POST request mode
Third Scenario: Set the decoding mode to UTF-8 before getting the request parameters
Examples are as follows: Request.setcharacterencoding ("UTF-8");
Advantages: Simple
Cons: Valid for post requests only, not valid for GET request mode
Two. The browser end receives the server side transmits the data to appear garbled the question!!! (garbled when retrieving data from the browser side)
1. Why does the data that the servlet returns to the browser appear garbled?
Servlet is returned by default, the JSP page is encoded in iso-8859-1 mode, the browser side of the data received with the server-side is also iso-8859-1 way, at this time, although the server-side encoding and browser-side decoding method, but because The Iso-8859-1 method itself does not support the Chinese encoding and decoding, so the Chinese has produced garbled.
2. How can i solve the problem of garbled data returned to the browser by the servlet?
Adding a page encoding to UTF-8 before outputting the JSP page informs the servlet to encode using the specified character set UTF-8 before outputting the JSP page (instead of using the default encoding: ISO-8859-1), The data type and character set returned by the browser server are also notified, and the browser uses the character set returned by the server to decode the data accordingly.
Example: Response.setcontenttype ("text/html; Charset=utf-8");
garbled characters in JSP and garbled problem in servlet and solution (without Ajax)