Garbled problem Reason:
The default tomcat is encoded with iso8859-1 when the URL is transmitted.
Solution One:
When using the Get transport parameters, the Chinese in the parameter is converted to the URL format, that is, the use of UrlEncode and urldecode to transfer, using this method is to convert the Chinese into a% beginning with the encoding in the URL transmission.
When using this method, pay attention to two points.
1. The front desk uses the UrlEncode, the corresponding use UrlDecode in the backstage.
2. The content using UrlEncode is empty within the parameters. It is important to note that he will convert the equal sign and so on. Therefore, it is better to first transfer the parameters after the splicing. Instead of stitching the URLs together and then converting them.
Solution Two:
Configure Tomcat to use the corresponding encoding that supports Chinese in the URL transfer process. General domestic likes to use GBK or gb2312. I personally recommend using Utf-8
In Tomcat's/conf/server.xml file, locate the following line.
<connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443"/>
This line means using port 8080 to receive HTML requests. Here you can add a few parameters to configure different effects.
Uriencoding= "UTF-8" to set the encoding format for URL content when the URL is transmitted
Compression= "On" to turn on the compression function
Compressionminsize= "2048" enables compressed output content size, which defaults to 2KB
Nocompressionuseragents= "Gozilla, Traviata" for the following browsers, do not enable compression
Compressablemimetype= "Text/html,text/xml" compression type
For the solution garbled, change to the following line
<connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443" URIEncoding= "UTF-8"/ >
Turn:
After analysis, I have a more in-depth understanding of Tomcat's handling mechanism and httpservletrequest.
Get parameter values in 1.filter filter garbled
Here is the code for the filter in the server side to get the parameters:
Java code
- Public void DoFilter (ServletRequest arg0, Servletresponse arg1,
- Filterchain arg2) throws IOException, servletexception {
- String foo=arg0.getparameter ("foo");
- System.out.println (foo);
- Arg2.dofilter (arg0, arg1);
- }
When I enter "http//:localhost:8080/rest/test?foo= China" in the browser, the browser will automatically do the "China" Uri transcoding, due to the use of the Chinese language environment, the browser will "China" transcoding "%d6%d0%b9%fa". " D6d0 "," B9FA "respectively" in "," state "of the GBK code. Equivalent to the following operations in the Java language
Java code
- Urlencoder.encode ("China","GBK")
The URL that is passed to the server is actually "HTTP//:LOCALHOST:8080/REST/TEST?FOO=%D6%D0%B9%FA".
Because Tomcat decodes the URL by default and uses the Iso-8859-1 character set, as shown below
Java code
- Urldecoder.decode ("%d6%d0%b9%fa","iso-8859-1");
Because encoding and decoding use different character sets, the decoded string is definitely not correct, and it is garbled to get the parameter value using the following method.
Java code
- String foo=request.getparameter ("foo");
Get parameter garbled in 2.resteasy service method
Java code
- @GET
- @Path ("/test")
- Public void Hello10 (@QueryParam (value="foo") String foo) {
- System.out.println (foo);
- }
The mechanism for obtaining the request parameter Foo in Resteasy is slightly different from the previous filter. The value of the Foo parameter is obtained by a resteasy framework similar to the following processing.
Java code
- String params=request.getquerystring ();
- SYSTEM.OUT.PRINTLN (params); //foo=%d6%d0%b9%fa
- String encodedparams= Urldecoder.decode (params,"UTF-8");
- ......
The parameters obtained by getquerystring () are not decoded by Tomcat, but are decoded by the Resteasy framework, which can be garbled if the incoming parameter is not UTF-8 encoded.
3. Summary
The parameters obtained using the Request.getparameter method are decoded by the Web server.
Raw parameters that are not decoded can be obtained using request.getquerystring
There are 2 ways to solve the garbled problem caused by Tomcat decoding.
Modify the Tomcat configuration file settings decoding method
Server side for the obtained parameter to the new String (Param.getbytes ("iso-8859-1"), "page Specify encoding") conversion
Tomcat get Chinese garbled