Solution to java displaying garbled characters:
1. UTF8 solves the problem of JSP Chinese garbled characters
Generally speaking, at the beginning of each page, add :
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("UTF-8"); %>
charset=UTF-8 is used to specify the encoding method output by JSP to the client as "UTF-8";
pageEncoding="UTF-8", in order to allow the JSP engine to It can correctly decode JSP pages containing Chinese characters, which is very effective in LINUX;
request.setCharacterEncoding("UTF-8"); encodes the request in Chinese.
Sometimes, this still cannot solve the problem, and you need to deal with it like this:
String msg = request.getParameter("message"); String str=new String(msg.getBytes("ISO-8859-1"),"UTF-8"); out.println(st);
2. Tomcat 5.5 Chinese garbled code
Just put the %TOMCAT installation directory%/ webapps\servlets -Examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.class file is copied to your webapp directory/filters. If there is no filters directory, create one.
2) Add the following lines to your web.xml:
<filter> <filter-name>Set Character Encoding</filter-name> <filter-class>filters.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2. Solution to get method
1) Open the server.xml file of tomcat, Find the block and add the following line:
URIEncoding=”GBK”
The complete should be as follows:
<connector port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />
2) Restart tomcat, everything is OK.
3. xmlHttpRequest Chinese problem
GBK encoding used for page jsp
Code:
<%@ page contentType="text/html; charset=GBK"%>
For more java knowledge, please pay attention tojava basic tutorial column.
The above is the detailed content of Solution to Chinese garbled characters displayed in Java. For more information, please follow other related articles on the PHP Chinese website!