The simplest solution to Chinese garbled characters for passing struts values

Source: Internet
Author: User
Another new project is about to begin. It may still need to use the SSH framework, so I decided to repeat the use of SSH. In fact, the use of the framework is still very simple, we should focus more on learning the implementation principles of the framework.

The garbled problem occurs when struts transmits Chinese characters. In my opinion, the most concise solution is to do the following:

1. Set pageencoding to GBK or gb18030;

2. Add a filter to Web. xml;

< Filter >
< Filter-name > Gbkencoding </ Filter-name >
< Filter-class > Com. sshnews. Filter. gbkencodingfilter </ Filter-class >
</ Filter >
< Filter-Mapping >
< Filter-name > Gbkencoding </ Filter-name >
< URL-Pattern > /* </ URL-Pattern >
</ Filter-Mapping >


3. compile the Code : package COM. sshnews. filter;
Import JAVA. io. ioexception;
Import javax. servlet. * ;
Import javax. servlet. HTTP. * ;

Public class gbkencodingfilter implements filter {
private filterconfig config = null ;

Public VoidInit (filterconfig arg0)ThrowsServletexception {
This. Config=Arg0;

}

Public   Void Dofilter (servletrequest req, servletresponse res, filterchain FC) Throws Ioexception, servletexception {
If ( This . Config =   Null ){
Return ;
}
// System. Out. println ("filter running ");
Req. setcharacterencoding ( " GBK " );
FC. dofilter (req, Res );
}

Public VoidDestroy (){
}

}

OK. I personally think this is the most concise method. If the database is still garbled, refer to the following Article
JAVA to write MySQL Chinese garbled solutions

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.