Suggested solutions for encoding (Chinese and other garbled) issues when using JSP development applications (including reception

xiaoxiao2021-03-06  115

I often see someone asking my JSP asking me what to do, how is the Chinese who have entered the user with request, how is it garbled? I wrote Chinese characters to the database how to garble, and so on. Problems about Chinese characters. In fact, this problem is very simple, is it a Chinese character, or Japanese, or other double-byte language, we will treat it as a UTF-8. (1) Double-byte text in Request, let's use UTF-8 coding in the entire application, the reason why UTF-8 is not only for the above reasons, we know that Java is based on UTF- 8 above, so we choose UTF-8 should be true ^ _ ^ We first use UTF-8 coding to save us, if it doesn't matter if it is not used in UTF-8, but It is recommended to use UTF-8 after writing. And in .jsp: <% @ page contenttype = "text / html; charset = UTF-8"%> instead of <% @ page contenttype = "text / html; charSet = UTF-8"%> then in Web .xml Add below: ... set character encoding com.redv.projects.eduadmin.util.Filters.SetChacterencodingFilter < / filter-class> Encoding utf-8 set character encoding / * ...

Where 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 imports filter {

Protected string encoding = null; protected filterconfig filter;

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 ); // is this sentence is working, haha, it: Overrides the name of the character encoding used in the body of this request. This method beser (). }

// 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 .Equalsignorecase ("true")) {this.ie = true;} else f (value.equalsignore ") {this.ignore = true;} else {this.ignore = false;}

}

protected string selectencoding (servletRequest request) {

Return (this.encoding);

}

}

In this way, our REQUEST request is encoded in UTT-8, and you can use: Request.getParameter ("mykey") in the JSP program to get the UTF-8 encoded string, not like this: New string (Request.getParameter ("mykey"). GetBytes ("ISO-8859-1"), "GBK") to solve those garbled. (2) Database processing double-byte text http://www.upas.org/java/databaseEncodingProbleMSolution/ The other is to write to the database, we know that we can switch to such a URL when we use mysql. Chinese character code: JDBC: mysql: // localhost: 3306 / upas? Useunicode = true & characterencoding = GB2312, what do we do if we can't solve this like MySQL? Do we write this this time: 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.executeQuery (); 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 the database connection example boolean code = true; EncodingConnection CodingConnection = New EncodingConnection (CON, CODING, ISO-8859-1", "GBK"); // Get takeover After the database connection instance, the use of Con = CodingConnection.getConnection () after eNCodingConnection is re-encapsulated after ENCIDINGCONNECTION ("SELECT F3, F4 from TBL1 WHERE F1 =? And F2 =?") PSTMT.STString (1, f1); pstmt.setstring (2, f2); rs = pstmt.executeQuery (); string f3, f4; while (rs.next ()) {f3 = rs.getstring (1); F4 = rs.getstring (2);}} finally {// close resouces ...} Take a look, how do we only need to modify the place where you get the database connection, or even we can save it as a parameter Properties, change the Coding's Boolean value to set whether you are using automatic encoding conversion. Often we can use a Database class to encapsulate the GetConnection that gets database connections so we can get database connections from Javax.Sql.DataSource. At this time, we just need to modify our Database class, and don't have to search all the use rs.setstring (), RS.GetString () to join our encoded conversion code. Even when we use the con.createstatment () statement, even if our SQL statement contains Chinese characters or other double-byte characters, there is no problem: select Name, Gender from Student Table Where Class Like '% Computer%'

Ok, just say so much, welcome to have a good idea or a good solution, discuss together; in addition, what is the problem, welcome, I will do my best.

转载请注明原文地址:https://www.9cbs.com/read-125647.html

New Post(0)