Today, through the JSP to the back-end of the process, found that the background is garbled. It took two hours, finally solved, and now a record.
Background
Now there is a JSP page, the main content is the table, where the table of each row of the last column is the Operation section, click on the row can be modified or deleted.
For example, clicking "Modify" will pass the left-hand argument to the background. "Power Distribution Type" and "Distribution sub-type" Two columns look at the text, in fact, the number is transmitted; Parameter type this item does not participate in the upload; "Parameter encoding" and "parameter name" are two words, in addition, there is a UUID will be passed to the background. As shown in the following:
The contents of my upload are displayed on the address bar of the browser:
Paraid is the previously mentioned Uuid;devmaintype and devsubtype corresponding to the distribution of the main type and distribution sub-type, Paracode and Paraname Two is the focus of this article, that is, the parameter encoding and parameter name mentioned earlier.
Problem
After the URL has been submitted, these parameters will be passed to the background code, the background code will be transferred to another front-end JSP page, the parameters of the pass display. In this case, the backend code is only responsible for the relay, not the upload parameters for processing. But there was a problem on the display page:
(Because some of the other items are normal, there is no) the parameter code and the parameter name of the original Chinese are all messed up.
Ideas
Recall the entire process, the front-end JSP (code a) to get the data in a row of the table, and then pass the data to the browser (code b), b pass the parameters to the background C.
- First, ensure A to B, the parameters are not chaotic.
By adding Console.log () to the parameter, it is found that the transmission is in Chinese and check.
- Second, check whether C received or garbled. If it is not garbled, the reason is C causes the last display problem, if it is already garbled, the process of B to C is a problem.
Add a breakpoint to the background, found in the reception, it is already garbled.
Conclusion: B should back the pot.
Check your browser
Take a look at what happens when a passes data b. Open Chrome Developer Tool
Enlarge the URL part:
It can be found that Paracode and paraname two have become%e6%b5%8b and other words ....
The first line on the top of the chart above is also "view parsed", click to see:
Yes, it's true that I preach it. However, the browser encodes it like this:
Now can be sure, the browser to my URL inside the Chinese did some format encoding, it became%E6%B5 ... This way. What kind of coding is that? Through webmaster Tools One of the test, the final discovery is URL encoding.
Solve
Since the browser will be encoded in Chinese, it is better to not include Chinese when passing to the browser. But what about the data in the form taken by a? The Chinese can be encoded in a, the result of the code is%e6%b5%8b%e8%af%95, then the browser will no longer encode it.
Steps
In a, add two times to Paracode and Paraname encodeURI ()
In C, the connection to the received parameter is added once decode
Then look at the final display page again
These two Chinese finally can display normally:)
Question 1
Why is a two-time code in a, and only one decoding in C? This is because the Tomcat container has been solved by default code once.
Then ask again, since the container default already has decoded, that in a is added once the code, C does not decode can it?
May, may not be possible. Because the encoding used by the container to decode is the default encoding for the container, it could be UTF-8, possibly GBK, and possibly other messy coding methods. You cannot guarantee that the code is the same as yours. So this way can be garbled.
It is also possible to use this one-time encoding if it feels too annoying to encode two times. Here you need to change the default encoding of the container so that it is the same as the way you encode it. such as change into UTF-8. How to modify it? Server.xml in the Conf directory under the Tomcat installation directory:
Turn this line into
Question 2
Careful friend may find that, after the browser to the Chinese code, to C is clearly%e6%b5%8b%e8%af%95 this form, why C received is &æ&mirco ... Such an HTML escape character? Well, I don't know ...
Resources:
[1] About URL encoding (http://www.ruanyifeng.com/blog/2010/02/url_encoding.html)
URL pass parameter causes garbled characters