Home > Java > JavaBase > body text

Java page garbled solution

Release: 2019-12-10 17:10:36
Original
2237 people have browsed it

Java page garbled solution

The solution to the garbled code submitted to tomcat on the page is to configure it in tomcat/conf/server.xml (recommended: java video tutorial)

Taking tomcat6.0.32 as an example, you need to change the following code:

Xml code

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

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" />
Copy after login

If If the tomcat front-end has Apache or Nginx forwarding, you need to change:

Xml code

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

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" />
Copy after login

Chinese url garbled code Here is a solution that can be used in any application deployment environment. This method is divided into two steps:

1. Use the escape(encodeURIComponent(fieldValue)) method to encode on the client, for example:

title=escape(encodeURIComponent(title)); //这是js里的函数
url="<%=request.getContextPath()%>/print/printList!printTable.action?title="+title;
Copy after login

2. Use java.net.URLDecoder.decode on the server (getRequest().getParameter("title"),"UTF-8"), decode.

To transmit Chinese in these two URL addresses, you must encode and then decode.

编码:encodeURI(encodeURI("包含中文的串"))
解码:java.net.URLDecoder.decode("需要解码的串","utf-8");
Copy after login

JSP page garbled characters usually just need to specify the character set encoding at the beginning of the page using the following code. If it still doesn't work, then please use the following sentence to convert

str=new String(str.getBytes("ISO-8859-1"),"页面编码方式");
Copy after login

The encoding used by JAVA in network transmission is "ISO-8859-1", so it needs to be converted when outputting, such as:

String str=new String(str.getBytes("开发环境编码"),"ISO-8859-1");
Copy after login

To correctly display the Chinese after network encoding on the page, it must be similar to

Stirng str=new String(str.getBytes("ISO-8859-1"),"开发环境编码");
Copy after login

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Java page garbled solution. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!