Solutions to common character encoding problems in Java Web applications

Source: Internet
Author: User

The following are common coding issues for Java Web applications

1. Encoding of HTML pages

In Web applications, browsers usually decide what encoding to use, such as encountering content-type:text/html, based on the value of the HTTP Header:content-type. Charset=utf-8, the page uses UTF-8 encoding. However, considering the offline HTML (the user may save the page HTML to the local), open the offline HTML when the meta-specified encoding, of course, do not specify the default value, it is not specified sometimes garbled.

META tags

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

2. Encoding of the HTTP request

Usually browse when sending a request to the server, do not set the character encoding, on the server side for uniform encoding, you can use Request.setcharacterencoding ("UTF-8") in the filter to set the encoding. General browsers are using the default Iso-8859-1 character encoding, to solve the Chinese parameter garbled must be this step.

3. Encoding of the HTTP response

HTTP response can be set to output data to the browser using the encoding method is response.setcharacterencoding ("UTF-8") and Response.setcontenttype ("text/html; Charset=utf-8 "), if simply set the encoding, preferably with response.setcharacterencoding (" UTF-8 "), because Response.setcontenttype (" text/html; Charset=utf-8 ") is the corresponding character encoding for the specific MIME type. It is important to note that the encoding set here does not necessarily work for JSP pages, because JSP pages have their own way of setting character encodings, and have higher precedence.

4 Encoding of JSP pages

There are three ways, if there are both first and second, only one will take effect (if different),

And the first and second will cover the third type of

Finally note that when using 1) or 2) to declare the encoding type, if the page has other JSPs in it, for <%@ include file= "bb.jsp"%> and <jsp:include page= "bb.jsp"/> , assuming that the previous way of includebb.jsp is used in aa.jsp, then there can be no duplicate encoding in bb.jsp的声明,效果是AA.jsp和BB.jsp都使用AA.jsp中声明的编码方式进行编码。

如果是使用后一种include的方式的话,AA.jsp和BB.jsp中都可以有各自编码的声明。

1) <%@ page contenttype= "Text/html;charset=utf-8"%>

2) <%@ page pageEncoding = "Charset=utf-8"%>

3) Add the following settings in Web. xml

<jsp-config>

<jsp-property-group>
<url-pattern>*.jsp</url-pattern>

<page-encoding>UTF-8</page-encoding>
</jsp-property-group>

</jsp-config>

5. Settings for MIME in Web. XML and Weblogic.xml

You can specify a MIME map in Web. XML, and you can also specify the character encoding used by the corresponding type (the settings here can also use the HTTP as described in 3 Response.setcontenttype and response.setcharacterencoding), such as:

<mime-mapping>

<extension>html</extension>

<mime-type>text/html; Charset=utf-8</mime-type>

</mime-mapping>

The default MIME type and character encoding can be set in Weblogic.xml

<Container-discriptor>

<default-mime-type>text/html; Charset=utf-8</default-mime-type>

</container-discriptor>

And also

<charset-params>

<input-charset>

<resource-path>/*</resource-path>

<Java-charset-name>UTF-8</java-charset-name>

</input-charset>

</charset-params>

In general, an application in HTML, HTTP request, HTTP response and JSP pages best display to specify the same character encoding, the most convenient is all set to UTF-8, all characters are not error. The setting for MIME mapping is best set in Web. Xml. The settings for character encoding in WebLogic are generally not set (if you set the previous words to correct later).

Solutions to common character encoding problems in Java Web applications

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.