Garbled characters in jsp and servlet

Source: Internet
Author: User

In the development of jsp and servlet, a small example I made today shows Chinese garbled characters. I have used many methods and found that it still cannot be solved. Finally, I used a filter to end the process.

This method is summarized as follows: Use <% @ page pageEncoding = "GB2312" %> On the jsp page. The pageEncoding here is GB2312, so we will filter the web. the <param-value> GB2312 </param-value> used in xml is also GB2312. In a word: Is the above two can be consistent, such as the same as the UTF-8

I write the java code of the filter and the configuration of web. xml.

// -------------- Java code: CharsetFilter. java

Package com;

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 CharsetFilter implements Filter {
Private String charset = "GB2312 ";

Public void destroy (){

}

Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain arg2) throws IOException, ServletException {
Request. setCharacterEncoding (charset );
Arg2.doFilter (request, response );
}

Public void init (FilterConfig arg0) throws ServletException {
Charset = arg0.getInitParameter ("charset ");
System. out. println ("character filter enabled, current status:" + charset );
}
}


// -------------- Web. xml section (the following section can be copied and pasted to WEB. XML)

<Filter>
<Filter-name> CharsetFilter </filter-name>
<Filter-class> com. CharsetFilter </filter-class>
<Init-param>
<Param-name> charset </param-name>
<Param-value> GB2312 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-name> CharsetFilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>

Source: http://blog.csdn.net/lvlingwy
 
 

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.