Web garbled Solution

Source: Internet
Author: User

  

Occasionally encountered in the project JSP into the background of the Chinese garbled and confused. The following will explain the occurrence of Chinese garbled in several cases.

    1. The JSP page does not have the encoding format set,
    2. The background does not have a character encoding filter configured in Web. Xml.
    3. When Ajax commits, it is submitted with a GET,
    4. You can also try to add a character encoding when connecting to a database.
    5. All foreground and background character encodings must be consistent.

JSP page does not have encoding format JSPMedium "CharSet"and"pageencoding"The Difference

Pageencoding: Represents the page encoding, is to set the JSP page source code character encoding format, if the value of the item is Utf-8, then the JSP source code can not write Chinese characters, if you use Eclipse and other tools, save when he will be prompted to have a mistake, change to GBK is OK.

CharSet: The character encoding, is the request server after the return of the content of the character encoding, even if pageencoding set GBK, save, run the program, viewing the page will find that the Chinese characters just written can not display properly, the charset changed to GBK, just fine

Note: When setting the JSP page source code character encoding, if there is pageencoding this item, then take the value of this item, if not, take the value of CharSet, if none, take iso8859-1. The Presets of pageencoding and CharSet are iso8859-1. And one of them is set, and the other is the same (TOMCAT4.1.27). But this is not absolute, it depends on how the respective JSP containers are handled. For example, if you set the pageencoding in the JSP in Tomcat, then ContentType is set to the same encoding, but not in resion, resin will use the default, by looking at the compiled class servlet Java files can see this, and the problem is precisely here, so, in the JSP, if it is resin under the best still explicitly set these 2 properties.

Jsptwo coding and three stages

JSP to pass two times the "code", the first stage will use Pageencoding, the second stage will use Utf-8 to Utf-8, the third stage is the Web page returned by Tomcat, with the CharSet.

The first stage: compiling the JSP into. Java, which reads the JSP according to Pageencoding's settings, and the result is translated into a unified UTF-8 by the specified encoding scheme Java source code (ie. java), if the pageencoding is set wrong, or not set, out is the Chinese garbled, or can not compile.

The second stage: from Javac Java source code to Java bytecode compilation, regardless of the JSP writing time with what encoding scheme, after this stage of the results are all UTF-8 encoding Java source code.

Phase three: Tomcat (or its application container) loading and executing phase, the output results, that is, seen on the client, this time hidden in phase one and phase two of the parameters charset to play a function.

Summary: Usually we set <%@ page contenttype= "text/html;charset=gb2312"%> on the JSP pages.

Configuring a character encoding filter in Web. xml

The order of the filters to be aware of when configuring the encoding filter in Web. XML, usually places the code at the beginning of the Web. XML, because the interception is in order and is easily intercepted if placed behind.

UsingStruts2in the Encoding filter configuration:

<!--ZH-CN Encoding--
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
Org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>


<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

With springin the Encoding filter configuration:

<!--Chinese garbled solution--
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Set character encoding when database connection

For example:

Url= "Jdbc:mysql://localhost:3306/laptop?useunicode=true&characterencoding=utf-8"

Request string encoding configured in Tomcat

By default, if the WebService or Web site that is deployed in Tomcat needs to have Chinese request parameters, and when we enter Chinese directly in the browser then the acceptance will be garbled, unable to meet our needs, At this point we need to set up the encoding format for Tomcat's request link.

Under Tomcat there is a conf directory with a server.xml configuration file, which has the following configuration for each port:

<connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000"
redirectport= "8443"/>

We need to add uriencoding= "UTF-8" at the end of this configuration, that is, the final form is:

<connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000"
Redirectport= "8443" uriencoding= "UTF-8"/>

Note: Some people say uriencoding= "UTF-8" must be added to the last side, otherwise invalid, I did not find the problem when testing, but in case or it added in the back of it, but also after modifying the Server.xml file, you must restart the service. This configuration is only valid for a get type of request and is not valid for a POST request. The parameter encoding for the POST request is still "iso8859-1", not "UTF-8"

You can use this method when the following scenario occurs: After the form has been filled in with information, the submission is printed in the console, and if the form is garbled with a get commit, and the console print appears normal after using post submission, you may try using the Modify the Server.xml file under Conf path in Tomcat.

Web garbled Solution

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.