How to Fix Chinese Character Garbling in JSP, Servlets, and Java Web Apps
This guide explains five common scenarios where Chinese characters appear garbled in Java web applications—JSP pages, servlets, POST form submissions, URI parameters, and properties files—and provides step‑by‑step configurations such as setting pageEncoding, response content type, request character encoding, Tomcat URIEncoding, and using native2ascii.
Case 1: JSP page shows garbled Chinese
Problem: When a JSP page is accessed via a browser, Chinese characters are displayed as unreadable symbols.
Solution: Ensure the JSP file is saved with UTF‑8 encoding in the editor, then add the directive at the top of the page: <%@ pageEncoding="utf-8" %> This forces the JSP compiler to treat the source as UTF‑8 and resolves the garbling.
Case 2: Servlet response shows garbled Chinese
Problem: A servlet outputs Chinese text, but the browser displays it incorrectly.
Solution: Before writing any output, set the response’s content type with UTF‑8 charset: response.setContentType("text/html;charset=utf-8"); This tells the browser how to decode the bytes.
Case 3: POST form parameters are garbled
Problem: Parameters submitted via a form (POST) to a JSP or servlet appear as garbled Chinese when retrieved.
Solution: In the receiving JSP/servlet, set the request character encoding before reading parameters: request.setCharacterEncoding("utf-8"); Then retrieve parameters normally, e.g.: <%=request.getParameter("username")%> Using a UTF‑8 filter is also recommended for a more robust approach.
Case 4: URI (GET) parameters are garbled
Problem: When parameters are passed via the URL (GET), Chinese characters become garbled because the default encoding is ISO‑8859‑1.
Solution: Modify Tomcat’s server.xml connector to specify UTF‑8 for URI encoding. Change the connector line to include URIEncoding="utf-8":
<Connector port="8080" protocol="HTTP/1.1" maxThreads="150" connectionTimeout="200000" redirectPort="8443" URIEncoding="utf-8" />This makes Tomcat decode query strings using UTF‑8.
Case 5: Properties files used for i18n are garbled
Problem: When creating .properties files for internationalization, Chinese characters appear as garbled text because the Java compiler expects ISO‑8859‑1.
Solution: Convert the properties file with the JDK’s native2ascii tool:
native2ascii -encoding utf-8 display.properties display_zh_CN.propertiesThis converts UTF‑8 Chinese characters to Unicode escape sequences that Java can read correctly.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
