Three methods for displaying Chinese Characters in JSP Web programs

Source: Internet
Author: User

Method 1: JSP development is the simplest and most commonly used method.

 
 
  1. <%@ page language="java" pageEncoding="GBK" %> 

Or you can use gb2312 or gbk here, But gbk supports more than gb2312 characters.

This method is used to display Chinese characters on the jsp page.

Method 2: Use a filter

Filters are mainly used for form submission. Are the data inserted into the database? . This is also for tomcat not according to the request specified encoding, or self-made adopts the default encoding mode iso-8859-1 encoding.

Compile a SetCharacterEncodingFilter class.

 
 
  1. Import java. io. IOException;
  2. Import javax. servlet. Filter;
  3. Import javax. servlet. FilterChain;
  4. Import javax. servlet. FilterConfig;
  5. Import javax. servlet. ServletException;
  6. Import javax. servlet. ServletRequest;
  7. Import javax. servlet. ServletResponse;
  8. Public class SetCharacterEncodingFilter implements Filter {
  9. Protected StringEncoding=Null;
  10. Protected FilterConfigFilterConfig=Null;
  11. Protected booleanIgnore=True;
  12. Public void init (FilterConfig filterConfig) throws ServletException {
  13. This. filterConfig= FilterConfig;
  14. This. encoding=FilterConfig. GetInitParameter ("encoding ");
  15. StringValue=FilterConfig. GetInitParameter ("ignore ");
  16. If (Value= Null)
  17.  This. ignore=True;
  18. Else if (value. inclusignorecase ("true "))
  19.  This. ignore=True;
  20. Else
  21.  This. ignore=False;
  22. }
  23. Public void doFilter (
  24. ServletRequest request, ServletResponse response, FilterChain chain)
  25. Throws IOException, ServletException {
  26. // TODO automatically generates method stubs
  27. If (ignore (request. getCharacterEncoding () = null )){
  28. StringEncoding=SelectEncoding(Request );
  29. If (encoding! = Null)
  30. Request. setCharacterEncoding (encoding );
  31. }
  32. Chain. doFilter (request, response );
  33. }
  34. Public void destroy (){
  35. // TODO automatically generates method stubs
  36.  This. encoding=Null;
  37.  This. filterConfig=Null;
  38. }
  39. Protected String selectEncoding (ServletRequest request ){
  40. Return (this. encoding );
  41. }
  42. }

Then, add web. xml

 
 
  1. <!-- Set Character Encoding-->  
  2.  <filter>  
  3. <filter-name>Set Character Encoding</filter-name>  
  4. <filter-class>com.struts.common.SetCharacterEncodingFilter
  5. </filter-class>  
  6. <init-param>  
  7. <param-name>encoding</param-name>  
  8. <param-value>UTF-8</param-value>  
  9.  </init-param>  
  10. </filter>  
  11. <filter-mapping>  
  12.  <filter-name>Set Character Encoding</filter-name>  
  13. <url-pattern>/*</url-pattern>  
  14.  </filter-mapping>  
  15. <!-- Set Character Encoding--> 

JSP development has many advantages in using filters, especially in projects.

And it is more useful when using internationalization, as long as the page specifies <% @ page language = "java" pageEncoding = "UTF-8" %>, the server displays the correct character set according to the local Locale.

Therefore, we recommend that you use filters.

Method 3: Modify URIEncoding In the tomcat server. xml file.

 
 
  1. <Connector debug="0" acceptCount="100" connectionTimeout="20000" 
    disableUploadTimeout="true" port="80" redirectPort="8443" 
    enableLookups="false" minSpareThreads="25" maxSpareThreads="75"  
    maxThreads="150" maxPostSize="0" URIEncoding="GBK" >
  2. </Connector>  

This method is mainly used to obtain strings from URLs.

JSP is developed in tomcat5.0 and later versions. The post and get methods are different in coding. If you get Chinese characters in the url, what will happen? . But there is no problem in tomcat4.1, because the post and get methods of tomcat4.1 are the same in encoding.

  1. Jsp xml xslt converts output to HTML
  2. Analysis on single processing and multi-task processing of JSP Technology
  3. JSP variables are called range variables in the specification.
  4. Detailed explanation of JSP technology methods
  5. JSP variables are called range variables in the specification.

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.