Struts,ajax appear garbled solution _ Common Tools

Source: Internet
Author: User
Tags response code

Garbled problem as if with our Chinese programmers special affinity, has been bothering us, from the beginning of the JSP garbled problem, struts garbled problem, to the present problem of Ajax garbled, no one is not a lot of programmers burned, all day to scold XXX products to Chinese support, UTF-8 can not use the Chinese ah what, in fact, the products are scolded in fact, more than 99% of the product is very good for Chinese support, and the reason for the garbled only because of their own internationalization support and file encoding information such as the knowledge of the cause. To know that a product is so popular, how can not support Chinese, the following start a group of people to solve these problems.

1 , coding
-- want to solve the Chinese problem, the code must not be all understand, coding is to solve the problem of Chinese garbled.
more commonly used in coding are: UTF-8 , GBK , GB2312 , Iso-8859-1 , except Iso-8859-1 Three other encodings are good for Chinese, but they are compatible Iso-8859-1 encoding (that is, no matter how the code changes, as long as it isIso-8859-1The characters in, will never appear garbled).

in these four encodings, GB2312 is the encoding of Chinese regulation, it can be said that the character set code of Simplified Chinese ; GBKis GB2312 the extension , In addition to compatible GB2312 , it also displays traditional Chinese and kana in Japanese. ; and UTF-8 Although it supports Chinese, it isGBcode incompatibility (different encoding values) . UTF-8 The variable length is used .Unicodeencoding, encoding may be1bit16into the system (that is, Iso-8859-1The characters in the code are also the same) and may be2bit or 3bit of16into the system. UTF-8 The advantages are:

1 , with Cpubyte order Independent,can be exchanged between different platforms.
2 , high fault-tolerant ability,after any one byte is corrupted,a maximum of one code bit loss will be caused ,No chain lock error
( as GB the code wrong one byte will be whole line of garbled utf-8 as encoded.

2, file encoding
      Although the correct encoding can be used to make the character display correctly, if you omit the encoding of the file when it is saved, it will let you into the fog.

      file encoding is most commonly used in two ways: ANSI and UTF-8, you can guess by the name, ANSI is the default encoding that we use when we save the file, and UTF-8 needs to set it ourselves. For code changes, the tools I use are Notepad and Eclipse,notepad, which is the easiest to use, as long as you open the file and select the appropriate encoding in the save, and it's very good for coding, and in Eclipse, just set up a little bit and open your preferences, Then select: General-> Content Type (ContentType), on the right, select the file type you want to change to save the encoding, and then change the value in the default encoding below, and then click the Update button.


In other editors, the default saved content is either GB2312 or GBK (the corresponding ANSI in Notepad). And according to the previous mentioned UTF-8 and gbk,gb2312 and other coding values are different this point, you can know that if the file used UTF-8, Then the character encoding must use UTF-8, otherwise the encoding value may cause garbled characters. And this is why so many people use the UTF-8 code will also produce garbled root cause. (JS and JSP are the reason)

3, jsp,struts, such as the Chinese garbled solution
In fact, there is only one way to solve this problem:

request.setcharacterencoding (encoding);

There's only one way to do it, but it's a variety of ways for beginners to use directly on JSP pages, and experienced programmers use filters. And now the way to say it is also a filter. This is illustrated by the unified use of UTF-8 as an example of encoding. The specific process is not much to say, there are many tutorials on the internet. Be lazy and copy it to Tomcat. In the Tomcat directory \webapps\jsp-examples\web-inf\classes\filters\ find the Setcharacterencodingfilter.java class, put it in your program and configure the mapping path. After configuration, basically your garbled problem is solved. But to map the path you need to be aware that you cannot use the ' * '

< filter-mapping >
< filter-name > Set Character Encoding </ filter-name >
< servlet-name > * </ servlet-name >
</ filter-mapping >

such as the above configuration, such as the Internet is probably the practice of most of the tutorial, think that is also harmful to me, you may only JSP garbled solution, to resolve the garbled struts need to map *.do or servletactionname. You can then set the value of the encoding in the initialization parameter.

< Init-param >
< param-name > encoding </ param-name >
< param-value > UTF-8 </ param-value >
</ init-param >

Of course, the most important thing to remember is to change the encoding of the editor you use to save the file in the same way that you use the character encoding.
In the JSP content, or use such as the online tutorials that the kind of technology, in all the pages of the top of the page to add:

<% @ Page Language ="java" contentType="text/html; CharSet=UTF-8"pageencoding=" UTF-8" %>

At this point, I believe jsp,action are not likely to appear garbled.

4, the resource file of the garbled solution
Resource file Everyone knows is an indispensable part of internationalization support, if the resource files are garbled that also have? In fact, the resource file is very good to solve the garbled, the reason is because the use of UTF-8 as a JSP code, no corresponding change in the file encoding of resource files, so as long as the resource file to save the code to correct, garbled problem will be solved. Of course, your Chinese should use the NATIVE2ASCII command to make the correct conversion.

5, the call JS, JS content garbled solutions.
In fact, JS garbled or file with the encoding of the relationship, if the JS in Chinese, the JS file to save the code must be the same as the page to call this JS, otherwise, all of your Chinese will be from the JSP page to JS will show normal. You can see that for the call JS appears garbled is the easiest to solve (also built in front of the hard work).

6, Ajax submitted data garbled, return data garbled solution
With the popularity of Ajax, garbled problems began to haunt many people just started to use it programmers, fortunately I have a bit of JSP garbled before the study, in the face of Ajax, and did not bring me much trouble, here will be some of my experience to share.

Same, the garbled problem of Ajax is naturally related to coding, in fact, many people like me think of the file encoding to set up, and in the data to set the Requet code, in the returned data set the response code all thought would be smooth, but all this is futile , nasty garbled again appear in front of your eyes. After you tried n a variety of methods, including JS own Escape,unescape method, you found garbled still appear rampant in the screen.

In fact, after trying this n many methods, many people did not find that the solution is actually very simple, and its answer in our previous processing JSP garbled. Let's look at the classic Ajax request code first.

Xmlhttp.open ("Post", url, async);
Xmlhttp.setrequestheader ("Content-type", "text/html" );
Xmlhttp.send (params);

Through the previous instructions, do not know you now see the clue is not. Do not know is affected by the online tutorials or other aspects of the impact, setRequestHeader and is a constant, and no one wants to change it, and the problem is just out of this place. Recall the encoding setting for the content of a JSP page, which has this section:

Contenttype= "text/html; Charset=utf-8 "

Now that we know the problem, we have to change the second code to read:
Xmlhttp.setrequestheader ("Content-type", "text/html;charset=utf-8");

Finally, don't forget to set it up when you return the data:

Response.setcontenttype ("Text/xml");
Response.setcharacterencoding ("UTF-8");

Is it simple and not troublesome at all?

If you want to ask why, in fact, we can see XMLHTTP as a temporary page, which is dynamically generated by the browser, the main role is to obtain the requested data in the background (can be seen as a high-level iframe). So for the normal page to set the encoding, it should also set the same. And in the servlet to return the data why to set contenttype and encoding is the same reason. As we all know, the final form of JSP is the servlet, and the JSP page the first set of the content is to let the generated servlet generated so two words:
Response.setcontenttype ("text/html");
Response.setcharacterencoding ("UTF-8");


Pageencoding, however, tells the JVM what encoding to use for this page's content (which is related to the generated class later). So it's a matter of course to set the response code in the servlet.

At one breath of their own year has encountered garbled problems and solutions to write out, hoping to help you.

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.