* Emphasis content *javaweb Project JSP load static resources appear Chinese garbled
First of all, I introduce myself in the code encountered a garbled problem: JSP page load, load the static resources of Chinese name, appear garbled, naturally get not to request resources, can not be aggravated, JSP page is not garbled. But the background is configured with the Code filter, why will garbled it.
Search the answer online, although found a way to solve the problem, but did not understand why. first look at the garbled solution
HTTP://BLOG.CSDN.NET/HAITIANXIAOWU1/ARTICLE/DETAILS/52460105 (reprint), this blogger's coding solution, for my rookie enough.
First, understand common sense
1.utf-8 International Code, GBK Chinese code. GBK contains GB2312, that is, if the GB2312 encoding can be decoded through the GBK, the contrary may not be established;
2, Web tomcat: Default is iso8859-1, does not support the Chinese
3.java.nio.charset.charset.defaultcharset () obtains the platform default character encoding;
4.getBytes () is encoded by the platform default character set;
Second, introduce
In learning any technology, often beginners encounter Chinese garbled problems, such as MySQL, because the installation is not set up, and in the servlet, you will encounter Chinese garbled problem;
Like what:
OutputStream out = Response.getoutputstream ();
Out.write (String);
The output of Chinese may appear garbled;
Output garbled problem is the program with UTF-8 encoding, and the browser by default with GBK decoding, so there will be garbled;
Three, the servlet related several garbled
1, browser calls jsp,html and other pages in Chinese display garbled
1 The file itself is saved as a utf-8 edit (myeclipse the right mouse button in properties to select Utf-8)
(2) browser with Utf-8 resolution:
(manual) ==> in the browser, select the encoding format for Utf-8
(SMART) ==> writes in a file such as: Simulate response header by tag, play to tell the browser to parse with UTF-8 encoding
(intelligent) ==> Response.setcontenttype ("Text/html;charset=utf-8"); to tell the browser to parse with UTF-8 encoding
==>get Request (URI mode passing parameter garbled): Main related content
Appearance: Browser access
Such as: Display user name and password
Workaround: The essence of the problem is the default encoding of the parameter content passed by the Get method to ask Iso8859-1, and the use of request.setcharacterencoding ("Utf-8") can not solve the problem.
Law one: To resolve this problem, modify the Tomcat server's configuration file. Modify line 43rd of the Conf/server.xml file in the Tomcat directory:
Before you modify the contents:
<connector port= "8080" protocol= "
http/1.1" maxthreads= "connectiontimeout=" redirecport= "8443"/>
Modified content:
<connector port= "8080" protocol= "
http/1.1" maxthreads= "connectiontimeout=" Redirecport= "8443" uriencoding= "Utf-8"/>
Law II: String usernamestring = new String (Username.getbytes ("iso-8859-1"), "UTF-8");
Borrowed from a pile of other people's things, the following explains I configured the encoding filter why there will be garbled in Chinese reasons:
When the JSP page loads, the static resources introduced by SRC are loaded, the request is a get, and the request of the gets method is called by Tomcat, and the default is Iso-8859-1, and then the coded filter that is configured in the background is parsed, Because the request has been resolved by Tomcat, the encoding filter resolves the request address again, it will appear in Chinese garbled.
Personal understanding, if there is a mistake, I hope the great God advice, easy to learn