Solve the WEB application in Tomcat5.0
Chinese problem
First, the JSP page garbled (Chinese display as question mark) Phenomenon: Chinese data generated by JSP page is displayed in garbled, but static HTML code is normal.
Cause: The default encoding mode of the JSP page in Tomcat 5.0 is ISO-8859-1. When the dynamically generated HTML code is encoded according to ISO-8859-1 (Western Europe characters), all of them are all in question mark.
Workaround: Plus in all JSP pages
<% @ Page contenttype = "text / html; charSet = GB2312">
Tomcat5.0 changes the encoding method to GB2312 when parsing the JSP page, and the Chinese shows normal.
Second, the Chinese garbled of the JSP page coming in in Include
Phenomenon: The Chinese display of the outer JSP page is normal, but the Chinese display of the JSP page (inner page) of the include INCLUDE is garbled.
Cause: After processing in the outer JSP page in the outer "method, the outer JSP page is correctly parsed according to the encoding method of GB2312, but the inner layer (ie, the JSP leaf surface that INCLUDE comes in) is still according to the default of Tomcat. The encoding method ISO-8859-1 is encoded, so the Chinese display of the inner layer page is not normal.
Solution: In the same time, plus in all inner layer JSP pages
<% @ Page ContentType = "text / html; charset = GB2312"> The Chinese display can return to normal.
Note: Two in the same JSP page cannot appear
<% @ Page contenttype = "text / html; charSet = GB2312">
Third, the Chinese garbled in form data submitted by POST mode
Phenomenon: Form data submitted by POST is garbled. Cause: Tomcat After receiving the request, TOMCAT did not set the correct encoding method according to the information in the request, but used the default encoding method ISO-8859-1 encoding, so the submitted form Chinese data is all question mark. Solve: You can add a filter that sets the character set, change the encoding method in the request to GB2312, Tomcat
Correctly submitted the data submitted by the POST. The contents of the filter are as follows:
package filters; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet. ServletResponse; import javax.servlet.UnavailableException; public class SetCharacterEncodingFilter implements Filter {protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; public void destroy () {this.encoding = null; this.filterConfig = null ;} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {// set the correct encoding if (ignore || (request.getCharacterEncoding () == null)) {String encoding = selectEncoding (request ); if (encoding = null) request.setCharacterEncoding (encoding);!} // passed to the next layer of filter chain.doFilter (request, response);} public void init (FilterConfig filterConfig) throws ServletException {this.filterConfig = FilterConfig; TH is.encoding = filterConfig.getInitParameter ( "encoding"); String value = filterConfig.getInitParameter ( "ignore"); if (value == null) this.ignore = true; else if (value.equalsIgnoreCase ( "true")) this.ignore = true; else if (value.equalsIgnoreCase ( "yes")) this.ignore = true; else this.ignore = false;} protected String selectEncoding (ServletRequest request) {return (this.encoding);}} ed After the filter, you have to configure a filter in Tomcat.
Open the web.xml file under the web-inf directory, add the following:
Phenomenon: In the JavaServlet, the Chinese garbled with PrintWriter is used. The HTML data received by the browser is ISO-8859-1 (Western European characters), then if the browser manually change the page encoding method to GB2312 Or GBK's Chinese display is normal.
Cause: The default encoding mode of Tomcat5.0 is ISO-8859-1, and the browser also sets the encoding method of HTML data to ISO-8859-1 when receiving the HTML data generated by the JSP page. The Chinese characters are displayed as garbled.
Solution:
Such a code below:
PrintWriter out = response.getwriter ();
Out.println ("");
Out.println ("
");Out.println (" HEAD>");
Out.write ("Error! Maybe it has expired !!");
Out.println (" html>");
First, the output is to put the Response Reset, I don't know why, reset is processed in the way below.
Then tell the browser that the page is to be encoded, add it.
Response.setContentType ("text / html; charSet = GB2312");
Tell the browser HTML data is encoded is GB2312, and then the output of each sentence is changed to ISO-8859-1 encoding, and the entire code is as follows:
Response.reset ();
Response.setContentType ("text / html; charSet = GB2312");
Out.println ("");
Out.println ("
");Out.println (" HEAD>");
Out.write ("error! Maybe it has expired !!". GetBytes (), "ISO-8859-1");
Out.println (" html>");
The Chinese output can be displayed correctly.
Fives. The Chinese data in the resource file is output to the JSP page.
Phenomenon: Displayed as garbled when the Chinese information pre-entered the resource file is output with the
Cause: Information for editing resource files If it is not a Western Europe, you must use the Native2ASCII tool to change its encoding to ASCII encoding.
Workaround: Native2ASCII is a tool with JDK, in your JDK directory / bin directory. The method of use is as follows
App.RES is a normal viewing Chinese resource file
ApplicationResources.properties to generate a resource file that can be displayed normally
Native2ASCII App.Res ApplicationResources.properties
After the conversion is completed, the Chinese information output using the