Java page Chinese garbled code solution

  • 2020-04-01 02:27:14
  • OfStack

The way to resolve the mess when the page is submitted to tomcat is to configure it in tomcat/conf/server.xml
Taking tomcat6.0.32 as an example, the following code is required:
Xml code

<Connectorport="8080"protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"/>
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />

To:
Xml code

<Connectorport="8080"protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"URIEncoding="UTF-8"/>
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
 

If there is Apache or Nginx forwarding in the tomcat front end, it is also necessary to:
Xml code

<Connectorport="8009"protocol="AJP/1.3"redirectPort="8443"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

To:
Xml code

<Connectorport="8009"protocol="AJP/1.3"redirectPort="8443"URIEncoding="UTF-8"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />

Here, we introduce a solution that can be used in any application deployment environment. This method consists of two steps:
1. Use the escape(encodeURIComponent(fieldValue)) method on the client side, for example:

title=escape(encodeURIComponent(title)); //This is a function in js
url="<%=request.getContextPath()%>/print/printList!printTable.action?title="+title;
 

2, on the server with java.net.URLDecoder.decode (getRequest (). The getParameter (" title "), "utf-8"), and decoding.

To transmit Chinese in these two url addresses, you must add the encoding, and then decode.

 Code: encodeURI(encodeURI(" Contains strings in Chinese ")) 
 Decoding: java.net.URLDecoder.decode(" A string that needs to be decoded ","utf-8");
 

JSP page scrambled code is usually as long as the following code at the beginning of the page to specify the character set encoding. If not, please use the following sentence to translate

str=new String(str.getBytes("ISO-8859-1")," Page encoding ");
 

The code used by JAVA in network transmission is "iso-8859-1", so the output needs to be converted, such as:

String str=new String(str.getBytes(" Development environment coding "),"ISO-8859-1");
 

After the network code after the Chinese, in order to correctly display on the page must be similar to

Stirng str=new String(str.getBytes("ISO-8859-1")," Development environment coding ");
 

This is the way to decode it

Related articles: