The best solution to Garbled text (struts struts2 jsp servlet action, etc)

Source: Internet
Author: User

The best solution to Garbled text:
1. Set the database encoding method to UTF-8.
2. The default encoding method of struts2 is UTF-8. That is struts. i18n. encoding = UTF-8.
3. Set the jsp page encoding method to UTF-8.

This eliminates the need to convert or filter characters each time:
1. character encoding filters.
1.1 set character encoding in a custom filter mode
Configuration in web. xml
<! -- Configure the struts2 filter -->
<Filter>
<Filter-name> struts2.x </filter-name>
<Filter-class> org. apache. struts2.dispatcher. FilterDispatcher </filter-class>
</Filter>
<! -- Configure the struts2 Chinese encoding filter -->
<Filter>
<Filter-name> CharacterEncoding </filter-name>
<Filter-class> com. cs. tb. util. CharacterEncodingFilter </filter-class>
<Init-param>
<Param-name> encoding </param-name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
Package com. cs. tb. 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;
/**
* It is used to set the HTTP request character encoding filter. The filter parameter encoding is used to specify the character encoding used to process the Chinese characters of the Html Form request parameters.
*/
Public class CharacterEncodingFilter implements Filter {
Private FilterConfig filterConfig;
Private String encoding = "";

Public void destroy (){
FilterConfig = null;
Encoding = null;
}

Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
If (encoding! = Null ){
Request. setCharacterEncoding (encoding); // sets the character encoding.
Chain. doFilter (request, response); // redirects requests and responses to the next link
}
}

Public void init (FilterConfig filterConfig) throws ServletException {
This. filterConfig = filterConfig;
This. encoding = this. filterConfig. getInitParameter ("encoding"); // obtain the initialization value encoding in the filter in the web. xml file.
}
}
1.2 use ActionContextCleanUp
Configuration in web. xml
<Filter>
<Filter-name> struts-cleanup </filter-name>
<Filter-class>
Org. apache. struts2.dispatcher. ActionContextCleanUp
</Filter-class>
</Filter>

<Filter-mapping>
<Filter-name> struts-cleanup </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>

2. request. setCharacterEncoding ("UTF-8 ");
3. str = new String (str. getBytes ("ISO8859-1"), "UTF-8 ");

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.