|
1.Encoding issues:
(1) First determine if the JSP page header has: <% @ page contentType = "text / html; charset = GBK"%>
(2) Transcoding like this:
String param = new String (request.getParameter ("param"). GetBytes ("ISO-8859-1"), "GBK");
(3) Add a filter character filter, the specific method:
First add the class:
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;
import org.apache.log4j.Logger;
import java.net.URLEncoder;
/ ** * // **
* Chinese string filtering in request
* /
public class SetEncodingFilter
implements Filter ... {
private Logger logger = Logger.getLogger (this.getClass ());
public void init (FilterConfig filterConfig) throws ServletException ... {
}
public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException ... {
logger.info ("Request transcoding filter ====================);
request.setCharacterEncoding ("gb2312");
chain.doFilter (request, response);
}
public void destroy () ... {
}
}
Register the class to XML:
<filter>
<filter-name> Set Encoding </ filter-name>
<filter-class> SetEncodingFilter </ filter-class>
</ filter>
<filter-mapping>
<filter-name> Set Encoding </ filter-name>
<url-pattern> / * </ url-pattern>
</ filter-mapping>
(4) If the parameters are passed through "a.jsp? Param = Chinese", you need:
a. Transcode the parameters before passing them: java.net.URLEncoder.encode (param);
Use java.net.URLDncoder.dncode (param) for the value; then switch back to Chinese
b. Find this section in your Tomcat directory-> conf directory-> server.xml:
<Connector
port = "8080" maxThreads = "150" minSpareThreads = "25" maxSpareThreads = "75"
enableLookups = "false" redirectPort = "8443" acceptCount = "100"
debug = "0" connectionTimeout = "20000"
disableUploadTimeout = "true" <!-Add this parameter here-> URIEncoding = "gb2312"
/>
(5) The database is garbled, first make sure that you are Chinese before inserting the database, the solution:
Add useUnicode = true&characterEncoding = GBK to the database connection URL of the database.
If garbled characters appear when reading, use the second method to solve it.
http://blog.csdn.net/lip009/archive/2006/09/07/1192022.aspx#521755 |
|