JSPWhat should I do if there are garbled characters in the Chinese text displayed? How can I use the user input in the request to get garbled characters, how can I write Chinese characters to the database, and so on. In fact, this problem is very simple, it is not Chinese characters, or Japanese, or what other double byte language, we will treat it as a UTF-8.
(1) double-byte text in the request
We come to implement the whole application using UTF-8 coding work, the reason for choosing UTF-8 is not only for the above reason, we know that java is based on the UTF-8, So we choose UTF-8 should be correct
First of all, the. java,. jsp files are saved with UTF-8 encoding, if the previous did not use the UTF-8 to save it does not matter, but it is recommended to Write later to save with UTF-8.
And write in. jsp:
The following is a reference clip: <% @ Page contentType = "text/html; charset = UTF-8" %> instead of % @ page contentType = "text/html; charset = UTF-8" % |
Add the following section in web. xml:
The following is a reference clip: <Web-app> ... <Filter> <Filter-name> Set Character Encoding </filter-name> <Filter-class> com. redv. projects. eduadmin. util. filters. SetCharacterEncodingFilter </filter-class> <Init-param> <Param-name> encoding </param-name> <Param-value> UTF-8 </param-value> </Init-param> </Filter> <Filter-mapping> <Filter-name> Set Character Encoding </filter-name> <Url-pattern>/* </url-pattern> </Filter-mapping> ... </Web-app> |
The code for com. redv. projects. eduadmin. util. filters. SetCharacterEncodingFilter is as follows:
Package com. redv. projects. eduadmin. util. 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; Import javax. servlet. http. HttpServletRequest; Import javax. servlet. http. HttpServletResponse;
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 { // Conditionally select and set the character encoding to be used If (ignore | (request. getCharacterEncoding () = null )){ String encoding = selectEncoding (request ); If (encoding! = Null ){ Request. setCharacterEncoding (encoding );// Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader (). } } // Pass control on to the next filter Chain. doFilter (request, response ); } Public void init (FilterConfig filterConfig) throws ServletException { This. filterConfig = filterConfig; This. encoding = filterConfig. getInitParameter ("encoding "); String value = filterConfig. getInitParameter ("ignore "); If (value = null ){ This. ignore = true; } Else if (value. inclusignorecase ("true ")){ This. ignore = true; } Else if (value. inclusignorecase ("yes ")){ This. ignore = true; } Else { This. ignore = false; } } Protected String selectEncoding (ServletRequest request ){ Return (this. encoding ); } } |
In this way, our request is encoded in UTT-8, In the JSP program can use: request. getParameter ("myKey") to get the UTF-8-encoded String directly without the need to look like this: new String (request. getParameter ("myKey "). getBytes ("ISO-8859-1"), "GBK") to solve those garbled.
(2) Dual-byte text processed by the database
Another issue is the database writing. We know that we can use this url to handle Chinese character encoding when using mysql: jdbc: mysql: // localhost: 3306/upas? UseUnicode = true & characterEncoding = gb2312, what if we cannot solve such problems as mysql? Do we write it like this every time: (www.bkjia.com)
Import java. SQL .*; Class. forName ("org. gjt. mm. mysql. Driver "); Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; Try { Con = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/test", "root ",""); Pstmt = con. prepareStatement ("SELECT f3, f4 FROM tbl1 WHERE f1 =? AND f2 =? "); Pstmt. setString (1, new String (f1.getBytes ("GBK"), "ISO-8859-1 "); Pstmt. setString (2, new String (f2.getBytes ("GBK"), "ISO-8859-1 "); Rs = pstmt.exe cuteQuery (); String f3, f4; While (rs. next ()){ F3 = new String (rs. getString (1). getBytes ("ISO-8859-1"), "GBK "); F4 = new String (rs. getString (2). getBytes ("ISO-8859-1"), "GBK "); } } Finally { // Close resouces ... } |
In fact, we can write like this:
Import java. SQL .*; Import com. redv. SQL. encoding .*; Class. forName ("org. gjt. mm. mysql. Driver "); Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; Try { Con = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/test", "root ",""); // Take over database connection instance Boolean coding = true; EncodingConnection codingConnection = new EncodingConnection (con, coding, "ISO-8859-1", "GBK "); // Obtain the database connection instance after taking over, and then use con directly to re-package the instance through EncodingConnection Con = codingConnection. getConnection (); Pstmt = con. prepareStatement ("SELECT f3, f4 FROM tbl1 WHERE f1 =? AND f2 =? "); Pstmt. setString (1, f1 ); Pstmt. setString (2, f2 ); Rs = pstmt.exe cuteQuery (); String f3, f4; While (rs. next ()){ F3 = rs. getString (1 ); F4 = rs. getString (2 ); } } Finally { // Close resouces ... } |
Let's take a look. We just need to modify it a little while getting the database connection. We can even save it as a parameter in properties, change the Boolean value of coding to determine whether automatic encoding conversion is used. We can often use a Database class to encapsulate the getConnection used to obtain the Database connection, so that we can obtain the Database connection from javax. SQL. DataSource. At this time, we only need to modify our Database class, instead of searching for all the rs used. setString (), rs. add our encoding conversion code to the getString.