Home  >  Article  >  Java  >  What to do if java objects are garbled

What to do if java objects are garbled

coldplay.xixi
coldplay.xixiOriginal
2020-08-17 11:41:102072browse

Solutions to garbled java objects: 1. When downloading garbled files, the download response header and filename must be encoded with URLEncoding before HTTP transmission; 2. For garbled files between java and the database, directly use unicode and To interact with the database, you can specify it in the driver's URL.

What to do if java objects are garbled

Solution to Java object garbled code:

Garbled code between jsp and page parameters

Forcefully specify the request encoding method:

request.setCharacterEncoding("UTF-8");

If the jsp output to the page appears garbled:

response.setCharacterEncoding("UTF-8");

Or configure the servlet filter filter in web.xml (only Valid for POST mode, invalid for GET mode):

<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>net.vschool.web.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Change Tomact configuration file, server.xml

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false"  URIEncoding="UTF-8">

URL processing:

<a href="TestAction.do?name=<%=java.net.URLEncoder.encode("你好","UTF-8")%>" ></a>

File download garbled code:

The key is the response header when downloading; filename must be encoded with URLEncoding for HTTP transmission.

response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName,"utf-8"));

Get method garbled code:

String args = new String(strCn.getBytes("ISO-8859-1"),"UTF-8");

Post method garbled code:

Just request.setCharacterEncoding(" UTF-8"); is enough.

Garbled characters between java and database

Use unicode directly to interact with the database, which can be specified in the driver's url, such as mysql driver:

 jdbc:mysql://127.0.0.1:3306/database?useUnicode=true&characterEncoding=utf-8

Related learning recommendations: java basic tutorial

The above is the detailed content of What to do if java objects are garbled. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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