Solve the problem of garbled character coding in Java EE development

There are many solutions to deal with character coding on the Internet. Here, I stand on the shoulders of predecessors and make my own summary.

In my opinion, the solution to the problem of garbled code is to set the code in three places:

1. Front end pages such as HTML and JSP;

2. Request and response objects in the background servlet;

3. Server configuration file.

1. When coding occurs on front-end pages such as HTML and JSP, first look at whether the meta tag of a front-end page has correctly set the coding. This meta tag is the head of the page

2. Background request and response settings

2-1. After receiving the URL request in the background, if the request is not encoded, the received request content will be garbled. At this time, it should be considered in two cases:

2-1-1. If it is a get request, get the parameters in the URL request first, for example:

Then the String object is called to complete the transformation of parameter encoding.

The string construction method here has two parameters: convert the parameter from the form of string to character array as the first parameter, and the second parameter is the encoding method of the converted string.

2-1-2. If it is a post request, add a code to set the code directly before obtaining the request parameters, that is, call the SetCharacterEncoding method of the request object to set the code:

2.2. After the background completes the business logic and persistence operations, it may be necessary to output the response stream data to the front station. If the output content contains Chinese, you need to set the code of the response object, and you can directly call the setcontenttype method of the response object:

3. Setting of server configuration file

If the current two steps are still ineffective after setting, it should also be in the conf folder under the installation directory of the server, such as e: \ tomcat7 0 \ conf, find the server configuration file server XML:

The above article to solve the problem of chaotic code in character coding in Java EE development is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>