Cause of jsp garbled characters under tomcat (below)

Source: Internet
Author: User

Cause of jsp garbled characters under tomcat (below)


Series of articles: Causes of jsp garbled characters in tomcat


In the previous article, I mainly explained the file encoding, page encoding, how to decode, and analyzed the role of tomcat. Here I am very familiar with the causes of jsp garbled characters in tomcat (I)

The reason for jsp garbled characters in tomcat is as follows.

1. Manual Decoding

2. setEncoding

3. get and post Methods

4. Configure tomcat Parameters

Okay. Let's take a closer look at the above methods.

1. Manual Decoding

This method has already been described in more detail in the previous article. The reason why we will repeat it here is mainly to make a comparison with other methods. For example:
String abc1 = new String(abc.getBytes("ISO-8859-1"),"utf8");  
This method decodes a string in the iso8859 encoding format into a UTF-8 string. The reason why this method is called a pure manual method is that you have to manually convert
String abc1 = new String(abc.getBytes("ISO-8859-1"),"utf8");
Is it very troublesome to write this sentence. "Lazy" programmers are good programmers: Are there any good solutions?


2. setEncoding

Let's review the source code of tomcat in the previous article.

    String encoding = getCharacterEncoding();      if (encoding == null)          encoding = "ISO-8859-1";      try{          RequestUtil.parseParameters(queryParameters, queryParamString, encoding);      }catch (Exception e){          ;      }  

Here we first see That tomcat sets the string to iso8859 by default, but it is when getCharacterEncoding () has obtained the value, so we have a simple way to solve this problem.

1. In String abc = request. getParameter ("abc"); previously, we could write and add a code to specifically set the request's encoding, so that tomcat is in the getCharacterEncoding ()

We can get the defined encoding. The Code is as follows:

 request.setCharacterEncoding("utf8"); String abc = request.getParameter("abc");

2. The abc in the above Code is directly UTF-8 encoded. If we submit a file that is also UTF-8 encoded before, we can obtain the correct encoding directly. Is it much simpler to encode each string in the first method? Furthermore, we can use a more effective filter method. You can define a filter to specifically implement this request. setCharacterEncoding ("utf8"); of course, in each jsp, or servlet, or

In the action, you do not need to process this encoding, which is more conducive to unified processing of encoding, of course, if you do more flexible, you can request. setCharacterEncoding ("utf8"); utf8 is used as a parameter.

Flexible and can be modified at any time without re-compiling the program.

Is it more in line with the lazy style of thinking? It is indeed much simpler than the first method. However, this is a double-edged sword. While being concise, it also hides a lot of content. Most people in many projects are business-oriented. If they are not involved, we may not be able to figure out the occurrence of such conversions. Therefore, as a programmer, we should improve our own quality, rather than simply complete the task. Well, this is beyond our scope. After talking nonsense, we will continue to come back to the topic.

We have already taken the initiative to convert encoding to unified encoding, so we can't help but think about it. Are there other methods? The answer is yes. tomcat has long considered these questions for us, but how much do we know about it? Next we will first make some preparations.

3. get and post Methods

The get and post methods of html should be listed here. Why? Let's take a look at two examples:

First, let's set the get method in test. jsp. We use httpwatch to observe what IE browser has done?

First, we set the post method in test. jsp. We use httpwatch to observe what the IE browser has done?

With the above 2 figures, we can clearly see the changes in the submission of test. jsp. One parameter is in the head, and the other parameter is in the body. (Note: This is also the reason why post data can be longer, while get is Limited)
With this basic understanding, let's go back to the topic and see how tomcat can help us solve this encoding problem.

4. Configure tomcat Parameters

Tomcat handles the get and post methods separately on this issue. You only need to modify the corresponding parameters.

The parameter location and name are config/server. xml

 
 

Add the following parameters:

URIEncoding = "gbk"

Set the url encoding for get.

Next we will demonstrate how to use it:

    
 


Here, we set the urlencoding of server. xml to gbk, and test. jsp encoding is also gbk. At this time, we told tomcat that my get url parameters are encoded using gbk. Take a look at the results:

When result. jsp is defined as follows:

String abc = request. getParameter ("abc"); if (abc = null) {out. println ("null");} else {out. println ("original encoding:"); out. println (abc); out. println (java. util. arrays. toString (abc. getBytes ("ISO-8859-1"); out. println (new String (abc. getBytes ("ISO-8859-1"); out. println ("
"); Out. println ("utf8 encoding:"); String abc1 = new String (abc. getBytes ("ISO-8859-1"), "utf8"); System. out. println (abc1); out. println (abc1); out. println ("
"); Out. println ("gbk encoding:"); String abc2 = new String (abc. getBytes ("ISO-8859-1"), "gbk"); out. println (abc2 );}


Corresponding code and results can be clearly seen, when we use the iso8859 Method for decoding, all errors occur. But there is no garbled code when getting the parameters directly.

Now let's change the test. jsp method to post, and the result. jsp method should not. Let's look at the result: <喎?"http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20141112/20141112090620165.jpg" alt = "">

We can see that the parameter settings in this region are effective for the get method.

I have written so much, but there are still a lot of details that seem to be unable to be involved and clarified at once. I will have the opportunity to talk about it later. Welcome


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.