Reprinted: http://blog.csdn.net/arielxp/archive/2004/06/23/23874.aspx
I found in tomcat5 that the previous method for processing tomcat4 could not be used to process requests submitted directly through URLs. I finally found the most perfect solution for finding information on the Internet, so I don't need to change it everywhere, and both get and post operations are normal. I wrote a document and posted it to people who want to have the same problems as me :-)
If there is a problem with uploading the file, I have to paste it directly. It's hard to see it :-)
-------------------
Tomcat 5 Chinese problems
Author: kiss__sky@163.com
-------------------
Problem description:
1. For the data submitted in the form, the string returned by request. getparameter ("XXX") is garbled or ??
2 directly through URL such as http: // localhost/a. jsp? Name = China, such a GET request is used on the server. garbled characters are returned when getparameter ("name") is getparameter ("name"). Filter setting based on Tomcat 4 is useless or request is used. setcharacterencoding ("GBK ");
Cause:
1 Tomcat J2EE Implementation of Form submission is POST method prompt processing parameters using the default iso-8859-1 to deal
2 Tomcat uses a different processing method than the POST method to process query-string requests submitted in get mode. (Unlike Tomcat 4, setcharacterencoding ("GBK") does not work.
Solution:
First, add the following to all JSP files:
1. Implement a filter. Set the processing character set to GBK. (There is a complete example in the webapps/servlet-examples directory of Tomcat. See the configuration of Web. xml and setcharacterencodingfilter .)
1) As long as % tomcat installation directory %/webapps \ servlets-examples \ WEB-INF \ Classes \ filters \ setcharacterencodingfilter. copy the class file to your webapp directory/filters. If there is no filters directory, create one.
2) Add the following lines to your web. xml:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3) complete.
2. Get Solution
1) Open the Tomcat server. xml file and find Add the following line to the block:
Uriencoding = "GBK"
The complete information should be as follows:
<Connector
port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true"
URIEncoding="GBK"
/>
2) Restart tomcat. Everything is OK.
Run the following JSP page to test whether the page is successful.
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. util. *" %>
<%
String q = request. getparameter ("Q ");
Q = q = NULL? "No value": Q;
%>
<HTML>
<Head> <title> display the news list </title>
<Meta http-equiv = Content-Type content = "text/html; charset = gb2312">
<Meta http-equiv = Pragma content = No-Cache>
<Body>
You submitted:
<% = Q %>
<Br>
<Form action = "tcnchar. jsp" method = "Post">
Chinese Input: <input type = "text" name = "Q"> <input type = "Submit" value = "OK">
<Br>
<A href = "tcnchar. jsp? Q = China "> submit via get </a>
</Form>
</Body>
If you enter a text box or click the hyperlink, the test result will be displayed: You submitted "China", indicating that it is successful !!!!!
Thank you very much for the following post and helped me solve the Chinese problem. Good luck !!!
Reference URL:
Http://www.javaworld.com.tw/jute/post/view? Bid = 9 & id = 44042 & sty = 1 & TPG = 1 & age = 0
Java Chinese has been plagued by many learners. Summarizes the solutions for the following situations.
I hope to help you.
Connect to MySQL Database Server:
-------------------------------------------------------------------------------
MySQL does not support Unicode, so it is troublesome.
Set connectionstring to encoding to gb2312
String connectionstring
= "JDBC: mysql: // localhost/test? Useunicode = true & characterencoding = gb2312 ";
Test code:
String STR = "Chinese characters ";
Preparedstatement pstmt = conn. preparestatement ("insert into test values (?) ");
Pstmt. setstring (1, STR );
Pstmt.exe cuteupdate ();
Database Table:
Create Table Test (
Name char (10)
)
Connect to Oracle Database Server
-------------------------------------------------------------------------------
Before inserting a Chinese character string into the database, perform the following conversion operations:
String (Str. getbytes ("iso8859_1"), "gb2312 ")
Test code:
String STR = "Chinese characters ";
Preparedstatement pstmt = conn. preparestatement ("insert into test values (?) ");
Pstmt. setstring (1, new string (Str. getbytes ("iso8859_1"), "gb2312 ");
Pstmt.exe cuteupdate ();
Servlet
-------------------------------------------------------------------------------
Add two sentences at the beginning of servlet:
Response. setcontenttype ("text/html; charset = UTF-8 ");
Request. setcharacterencoding ("UTF-8 ");