In the past two days, I finally started my first website for software development... In general, it was quite smooth. However, there was an episode, and I finally met the most "expected" garbled problem, garbled code, and garbled code, you are such a rookie-level god...
Now let's talk about the garbled information I 've encountered. My overall framework is a form on the front-end interface, and then I submit the information to the background action, it is then used to call a method to submit data to the database... Unfortunately, everything is not as perfect as I thought. At last, garbled characters still appear when the form is submitted to the action... Sorrow... But because of the garbled experience of a summer vacation, I feel less scared than I used... I thought that the previous method could be used to solve the problem, but it took a whole day and almost all the previous methods were used .. The problem persists...
I used to think it was a killer method: 1. Set the character encoding to UTF-8 in the header of the form page.
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
Then, set the encoding of the page request and response:
<% Request. setcharacterencoding ("UTF-8 ");
Response. setcharacterencoding ("UTF-8"); %>
Finally, write in the action:
Servletactioncontext. getrequest (). setcharacterencoding ("UTF-8 ");
Servletactioncontext. getresponse (). setcharacterencoding ("UTF-8 ");
After setting these settings, you will be done... But the final submission is still garbled .. Garbled characters .. No way. Continue to find a solution .. Finally, I remembered the legendary struts2 internationalization problem that I used before. I added this sentence before struts. xml:
<Constant name = "struts. i18n. encoding" value = "UTF-8"/>
Well, no matter how big the world is... I thought it would be okay if I could solve my problem .... The class is disappointing .. I have never seen any effect since I was International...
Garbled, garbled, you have made me a big head... No way. Continue to solve the problem .. Check it online... In the end, I can't help it. Try the filter I hate most ..
Specific process:
1. Compile the filter class:
Package org. Rn. util;
Import java. Io. ioexception;
Import javax. servlet. filter;
Import javax. servlet. filterchain;
Import javax. servlet. filterconfig;
Import javax. servlet. servletexception;
Import javax. servlet. servletrequest;
Import javax. servlet. servletresponse;
Public class encoding implements filter {
@ Suppresswarnings ("UNUSED ")
Private filterconfig Config = NULL;
String encoding = NULL;
Public void destroy (){
This. Encoding = NULL;
This. Config = NULL;
}
Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {
If (encoding! = NULL)
Request. setcharacterencoding (encoding );
Chain. dofilter (request, response );
}
Public void Init (filterconfig arg0) throws servletexception {
This. Config = arg0;
This. Encoding = arg0.getinitparameter ("encoding ");
}
}
2. Configure in Web. xml
<Filter>
<Description> cache filtering </description>
<Filter-Name> encoding </filter-Name>
<Filter-class>
Filter. Encoding
</Filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> encoding </filter-Name>
<URL-pattern> * </url-pattern>
</Filter-mapping> </filter>
After writing this, I was proud to think that everything was done, but he still had garbled code. I couldn't help it. I had taken out all the old shops that used to solve the garbled code and couldn't help it, I thought this time I was dead. At this time, thanks to the encouragement of some part-time team partners, Mr. Jin .. You need to perform online Taojin, check, and post on the Forum:
The following are my posts and replies
1 floor: I wrote a form .. The form data is submitted to the struts2 action... Then, the action is submitted to the database .. But the data is garbled when it reaches the database .... The database I use is sqlserver... Expert advice .... Try to believe in something ..... <% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> and <% request. setcharacterencoding ("UTF-8 ");
Response. setcharacterencoding ("UTF-8"); %>
Servletactioncontext. getrequest (). setcharacterencoding ("UTF-8 ");
Servletactioncontext. getresponse (). setcharacterencoding ("UTF-8 ");
2 floor: Database code
Third floor: details... How can I set the character encoding of sqlserver?, I won't... When Baidu is absent ,,,
4 floor: JDBC: mysql: // localhost: 3306/android_stat? Useunicode = true & characterencoding = UTF-8
When connecting to the database, add? Useunicode = true & characterencoding = UTF-8
5 th floor: add that... "JDBC: sqlserver: // localhost: 1433; databasename = ruiniinfodb? Useunicode = true & characterencoding = UTF-8 ";
The database cannot be connected...
Unable to open the Database "ruiniinfodb? Useunicode = true & characterencoding = UTF-8 ". Logon Failed.
6th floor: how can this problem be solved? I can't write a filter. It should be done.
7 floor: meet the following requirements and there will be no garbled characters.
1. The program file and JSP file are UTF-8 encoded. The UTF-8 encoding type is set for the header shake of the JSP file.
2. Set the database to UTF-8
You can see what your database encoding settings are. If the webpage is UTF-8 and the database is gb2312, it may be garbled and right-click the database to view the attribute.
8 floor; sqlserver sets database Encoding
9th floor: Generally, a character filter is configured in Web. xml during SSH integration.
<! -- Translated character encoding -->
Java code <filter>
<Filter-Name> encodingfilter </filter-Name>
<Filter-class> org. springframework. Web. Filter. characterencodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> UTF-8 </param-value>
</Init-param>
<Init-param>
<Param-Name> forceencoding </param-Name>
<Param-value> true </param-value>
</Init-param>
</Filter>
On the 10th floor: You are right about the top floor, that is, the database problem. The answer is as follows: the first method: select the database with an error. Right-click the database attribute. The database attribute dialog box is displayed. select the option in the database attribute dialog box. Set the sorting rule in the option to chinese_prc_90_ci_as. Click OK. (Note: When selecting database attributes, you must ensure that the database you modified is not used before modification. Otherwise, the modification will fail.) Method 2: first open your SQL query analyzer (new query), and then enter the following code to execute: Alter database name collate chinese_prc_90_ci_as for example:
Alter database 9npc collate chinese_prc_ci_aialter database autoupgrade collate chinese_prc_ci_ai resolution reason: The preceding problem occurs mainly because the database attribute sorting rules are incorrectly set. in Microsoft SQL Server 2005, the physical storage of strings is controlled by sorting rules. The sorting rule specifies the bit mode of each character and the rules used to store and compare characters. The name of a sorting rule consists of two parts. The first half is the character set supported by this sorting rule. For example, the first half of chinese_prc_90_ci_as indicates the Unicode Character Set, and the chinese_prc _ pointer sorts Unicode in simplified Chinese characters. The second half of the sorting rule is the suffix.
Meaning: _ bin binary sorting _ whether the Ci (CS) is case sensitive, CI is case insensitive, CS is case sensitive _ AI (AS) is case sensitive, and AI is case insensitive, as differentiation _ KI (KS) Whether to distinguish Kana type & defki not distinguish, KS differentiation _ wi (WS) Whether to distinguish width wi not distinguish, WS is case sensitive: select this option if you want to compare uppercase and lowercase letters. Accent differentiation: select this option if you want to treat the comparison as different from the accent and non-accent letters. If this option is selected, the comparison also treats letters with different accents as unequal. Kana differentiation: select this option if you want to treat Katakana and katakana as different Japanese syllables. Width differentiation: select this option if you want to make the comparison between halfwidth and fullwidth characters. This is from Baidu. How can I find the sqlserver2005 solution...
11 floor: SQL Server database Encoding Problems.
Ah, maybe I'm wrong... Unfortunately, the answer is all about the database... And many people are talking nonsense... Ah, no way .. Try again...
Haha, good luck. I met a female programmer: I am taught a new method to solve garbled code:
Author = new string (author. getbytes ("iso8859-1"), "UTF-8 ");
Content = new string (content. getbytes ("iso8859-1"), "UTF-8 ");
An said that this is not a new method... But he is familiar... I actually fixed the issue of garbled characters ,,,,
Follow this method... Two similar garbled questions are successfully solved. This method becomes invalid when the third similar garbled question is solved in class hours ....
After careful analysis, it is very likely that this form contains File Upload problems:
<Form action = "newaddaction" method = "Post" name = "form"
Enctype = "multipart/form-Data">
Well, that's the problem .. I tried it again and removed all the above methods to solve the Garbled text problem... Hey, who told us we were cainiao... Only repeated tests... I really don't know why this is used... Ah, cainiao's sorrow
Okay... Garbled. This article page should include a majority of methods to solve Garbled text...
For more information, refer to shoot bricks. For more information, see the source.