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
- <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:
- <jsp-config>
-
- <jsp-property-group>
- <url-pattern>*.jsp</url-pattern>
-
- <page-encoding>UTF-8</page-encoding>
- </jsp-property-group>
-
- </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:
- <mime-mapping>
-
- <extension>html</extension>
-
- <mime-type>text/html; charset=UTF-8</mime-type>
-
- </mime-mapping>
-
You can set the default mime type and character encoding in weblogic. xml.
- <container-discriptor>
-
- <default-mime-type>text/html; charset=UTF-8</default-mime-type>
-
- </container-discriptor>
And
- <charset-params>
-
- <input-charset>
-
- <resource-path>/*</resource-path>
-
- <java-charset-name>UTF-8</java-charset-name>
-
- </input-charset>
-
- </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 ).