Completely solve the problem of Chinese garbled java web development program

It will be several times the encoding

First, the browser will be conducted when the transmission data once encoded and then time will obtain these parameters in a container once decoded (e.g. tomcat).

Coding at the browser

First, in the address bar of the browser paragraph the following address: HTTP: // localhost: 8080 / zhangsan the Login name =? . This time because the request addresses and parameters are in line with the ASCII code, and the argument is not some special characters, such as: ":" "?", "/", And so on. So the browser will not be too much to deal with this url.

Gangster quoted passage: Since the URL, therefore, in order to ensure compatibility and interoperability of information when the URL contains non-ASCII characters must be escaped passed over the network.

But if there are Chinese or special characters when parameter: HTTP: // localhost:? 8080 / the Login name = Joe Smith . In the url above paragraph, the parameter value becomes the Chinese, this time to copy the URL address bar clip into Notepad will find url become like this: HTTP: // localhost: 8080 / the Login name =% E5? the BC% E4 A0%%%% 89 B8 . Wherein % E5% BC% A0% E4 % B8% 89 a seating in UTF-8 encoding . This is the process of Chinese browser.

URLEncoder can be used to test java class:

public static void main(String[] args) throws Exception {
        String text1=URLEncoder.encode("张三","UTF-8");
        System.out.println(text1);
        //输出:%E5%BC%A0%E4%B8%89
 }

 

Servlet Container parameter acquisition process 

As the above example, the browser to "John Doe" in the process of UTF-8 encoding. Get Servlet in this parameter, such as using: reqeest.getParameter ( "name"). If the container is used by default it is ISO-8859-1 encoding, equivalent to do the following operations.

public static void main(String[] args) throws Exception {
        String text2=URLDecoder.decode("%E5%BC%A0%E4%B8%89","ISO-8859-1");
        System.out.println(text2);
        //输出:å¼ ä¸
    }

 

This situation appeared garbled, because the client or browser encoding with service at the end as a result of different tomcat encoding.

Here's how to solve the case.

In other encoding format Servlet Container

The simplest way is possible to change the Servelt container (such as tomcat) encoding, so long as the coding of the browser with the same encoding containers, generally will not be garbled.

tomcat need to modify the root directory conf / ser.xml file

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

改成

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

 

Before tomcat7 and a default encoding is "ISO-8859-1", in the default encoding tomcat8 use are "UTF-8", so if using tomcat8 +, and the browser is UTF-8 encoded format generally do not need to be modified.

Let's see, the encoding format browser.

Browser POST and GET encoded differently to process the request

If the container does not modify Web default encoding format, and the processing in the Servlet garbled necessary to distinguish GET request and the POST request.

1, POST request

In the above example, using a GET request, he is placed directly behind the url parameter spliced ​​using a "?" Segmentation. POST request will be placed in the request body parameters, depending on Content-Type for encoding data (e.g., a browser may be provided Content-Type: text / html; charset = UTF-8), and then transmit the form data to the server.

<meta http-equiv="Content-Type"  content="text/html; charset=UTF-8"/>

 

At this time if the container using the "ISO-8859-1" or other non "UTF-8" decoding mode, the case distortion will occur.

This can be used when the setCharacterEncoding HttpServletRequest () method of encoding used to specify a POST request parameter acquisition.

The Browser If "UTF-8" POST request may be used in Servlet request.setCharacterEncoding time ( "UTF-8"); encoding parameter used to obtain a predetermined time (if the container is a default ISO-8859 -1).

Note : This method must be executed to take effect before obtaining request parameters, if the first acquisition parameters, then execute setCharacterEncoding ( "UTF-8") , it will still be the default encoding section code based on TOMCAT container.

 

2, GET requests

The above setCharacterEncoding () method will only be useful for the Request Body parameters. But using the GET request sent in the url parameter is the (process because the URL is an HTTP server, instead of the Web container, tomcat embedded in just a Http container), it will still use tomcat default encoding to convert.

This time can be handled using the String of getBytes (). The browser uses UTF-8 character processing, while ISO-8859-1 Web use containers.

Request.getParameter name = String ( "name" ); 
name = new new String (name.getBytes ( "the ISO-8859-1"), "UTF-. 8"); 
// this is the correct value of the name

 

 

If the encoding format unsure browser, or javascript jquery generally used to non-ASCII transcoding parameters specified format, and then back to the group as a url parameter.

 

Return is the same. Handle the encoding format with the browser returns the format will not resolve the problem garbled. . . .

Reference: Portal 1 , "Servlet & JSP study notes"


 

end...

 

Guess you like

Origin www.cnblogs.com/Eastry/p/12452408.html