Solutions to common character encoding problems in Java web Applications

Source: Internet
Author: User

The following are common coding problems for Java web applications:

1. html page Encoding

In web applications, browsers usually decide what encoding to use based on the value of http header: Content-type, for example, Content-Type: text/html; charset = UTF-8, the page uses UTF-8 encoding. However, considering the offline html (the user may save the html of the page to the local), the encoding must be specified when the offline html is opened. Of course, if this parameter is not specified, the default value will also exist, if this parameter is not specified, garbled characters may occur.

Meta tag

 
 
  1. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 

2. Http Request Encoding

Generally, when a browser sends a request to the server, it does not set the character encoding. on the server side, for uniform encoding, the request can be used in the filter. setCharacterEncoding ("UTF-8") to set encoding. the general browser is to use the default ISO-8859-1 character encoding, to solve the Chinese parameter garbled must be this step.

3. http response Encoding

In http response, you can set the encoding method for the data output to the browser to response. setCharacterEncoding ("UTF-8") and response. setContentType ("text/html; charset = UTF-8"), if you just set the encoding, it is best to use response. setCharacterEncoding ("UTF-8") Because response. setContentType ("text/html; charset = UTF-8") is the character encoding that is set for a specific MIME type. It should be noted that the encoding set here does not necessarily work for the JSP page, because the JSP page has its own character encoding method and has a higher priority.

4. JSP page Encoding

There are three methods, if both the first and second methods take effect, if they are different ),

There are also the first and second types that will overwrite the third

Note that when using 1) or 2) to declare the encoding type, if other JSP files are included in the page, for <% @ include file = "BB. jsp "%> and <jsp: include page =" BB. the difference between jsp "/> is assumed in AA. in jsp, use the previous method includeBB. jsp, so BB. there cannot be repeated encoding declarations in jsp. The effect is AA. jsp and BB. jsp uses AA. the encoding method declared in jsp.

If the last include method is used, both AA. jsp and BB. jsp can have their own encoding declarations.

1) <% @ page contentType = "text/html; charset = UTF-8" %>

2) <% @ page pageEncoding = "charset = UTF-8" %>

3) Add the following settings in web. xml:

 
 
  1. <jsp-config> 
  2.  
  3. <jsp-property-group> 
  4. <url-pattern>*.jsp</url-pattern> 
  5.  
  6. <page-encoding>UTF-8</page-encoding> 
  7. </jsp-property-group> 
  8.  
  9. </jsp-config> 

5. mime settings in web. xml and weblogic. xml

In the web. mime ing can be specified in xml, and character encoding can be specified for the corresponding type (the settings here can also use the http response mentioned in 3. setContentType and response. setCharacterEncoding), for example:

 
 
  1. <mime-mapping> 
  2.  
  3. <extension>html</extension> 
  4.  
  5. <mime-type>text/html; charset=UTF-8</mime-type> 
  6.  
  7. </mime-mapping> 
  8.  

You can set the default mime type and character encoding in weblogic. xml.

 
 
  1. <container-discriptor> 
  2.  
  3. <default-mime-type>text/html; charset=UTF-8</default-mime-type> 
  4.  
  5. </container-discriptor> 

And

 
 
  1. <charset-params> 
  2.  
  3. <input-charset> 
  4.  
  5. <resource-path>/*</resource-path> 
  6.  
  7. <java-charset-name>UTF-8</java-charset-name> 
  8.  
  9. </input-charset> 
  10.  
  11. </charset-params> 

In general, it is best to specify the same character encoding explicitly in html, http request, http response and jsp pages of an application, the most convenient is to set it as a UTF-8, no error occurs for all characters. It is best to set mime mapping in web. xml. Generally, you do not need to set the character encoding settings in weblogic. If you set all the preceding settings correctly ).

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.