Brief Introduction to JSP Chinese garbled Processing

Source: Internet
Author: User

1. garbled characters are displayed when the JSP page is called.
When a JSP page is called through a browser, all Chinese content in the client browser is garbled.

Solution:
First, confirm that the encoding format of GBK is used when the JSP file is saved in the editor, add <% @ pageEncoding = "GBK" %> at the beginning of the JSP page to solve Chinese garbled characters.

2. garbled characters are displayed when the Servlet page is called.
When Servlet is called through a browser, the Servlet displays garbled content in the browser.

Solution:
Before using response in Servlet to output content, execute response. setContentType ("text/html; charset = GBK") to set the encoding of the output content to GBK.

3. garbled parameters passed in the Post form
Submit parameters to the corresponding JSP page or Servelt through the JSP page, HTML page, or form Element in the Servlet. The Chinese parameter values received by the JSP page or Servlet are garbled.

Solution:
Before receiving parameters submitted by POST, use request. setCharacterEncoding ("GBK") to set the content of the received parameters to be encoded by GBK.

A better solution is to use the filter technology.

 
 
  1. Package com. htt;
  2. Import java. io. IOException;
  3. Import javax. servlet. Filter;
  4. Import javax. servlet. FilterChain;
  5. Import javax. servlet. FilterConfig;
  6. Import javax. servlet. ServletException;
  7. Import javax. servlet. ServletRequest;
  8. Import javax. servlet. ServletResponse;
  9. Public class Encoding implements Filter {
  10. Public void destroy (){}
  11. Public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  12. Request. setCharacterEncoding ("GBK ");
  13. Chain. doFilter (request, response );
  14. }
  15. Public void init (FilterConfig filterConfig) throws ServletException {}
  16. }
  17. Settings in the Web. xml file
  18. <Filter> 
  19.  <Filter-name>Encoding</Filter-name> 
  20. <Filter-class>Com. htt. Encoding</Filter-class> 
  21. </Filter> 
  22. <Filter-mapping> 
  23. <Filter-name>Encoding</Filter-name> 
  24. <Url-pattern>/ToCh_zn</Url-pattern> 
  25. </Filter-mapping> 
  1. Functions and principles of several encodings in JSP and Servlet
  2. Describes the usage of the request attribute in JSP
  3. Integration of Apache2.2 and Tomcat5.5 in JSP environment Configuration
  4. FAQs about Chinese JSP pages in Tomcat 4.0 and Tomcat 4.1
  5. Summary of redirection Technology in Servlet and JSP

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.