A solution for garbled code writing from JSP web pages to databases
Source: Internet
Author: User
Java is a good thing, and it feels cool to write Java. But when Java and databases are used, most of them will encounter garbled characters, especially Chinese characters are always raped into "?". It is more or less uncomfortable to see the psychological aspect. This is why we were bullied by foreigners because the software was written by them and supported English language by 100%. However, our characters were always thrown by them like a ball. I have heard that Microsoft has launched an OneCare anti-virus program. If I want to use it, I am prompted that the language is not supported and the word is "Angry ".
To correctly insert Chinese characters into the database, I tried n methods:
1, the current web page character into "ISO8859-1", a line, all into garbled, no;
2, because I use struts, the character encoding of the struts-config.xml from "gb2312" to "ISO8859-1", it is not good, print out all garbled;
3. On the action page, set the request character to "gb2312": request. setcharacterencoding ("ISO8859-1"), no;
4. On the action page, set the request character to "gb2312": request. setcharacterencoding ("gb2312"). No;
......
Finally, add a character conversion function to the bean page:
String format2is08859 (string Str ){
Try {
// Cname = new string (cname. getbytes ("gb2312 "));
STR = new string (Str. getbytes ("ISO8859-1 "));
} Catch (unsupportedencodingexception e ){
E. printstacktrace ();
}
Return STR;
}
You can use this function to convert the characters in other getxxx functions before using it.
Public void setcname (string cname ){
Cname = format2is08859 (cname );
This. cname = cname;
}
This problem has been solved, but I know it will take a lot of work in the future, because not only such a place requires character conversion, if you can set a character in that place at one time, then OK, this reduces the workload.
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.