Js|web| Program | solution | show | Chinese
method One: The simplest is the most used method
<%@ page language= "java" pageencoding= "GBK"%>
or <%@ page contenttype= "TEXT/HTML;CHARSET=GBK", where you can use gb2312 or GBK, but GBK supports more characters than gb2312.
This method is used for Chinese display in JSP pages.
method Two: Use the filter
Filters are used primarily for form submissions, and data is inserted into the database? Resolution This should also be the encoding that Tomcat does not specify for the request, or the default encoding for ISO-8859-1 encoding.
Write a Setcharacterencodingfilter class.
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;public class Setcharacterencodingfilter implements Filter {protected String encoding = Null protected Filterconfig filterconfig = null; Protected Boolean ignore = true; 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.ignore=true; else This.ignore=false; public void Dofilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, Servletex ception {//TODO automatic generation Method stub if (Ignore | | (request.getcharacterencoding () = null)) {String encoding = selectencoding (request); If(Encoding!= NULL) request.setcharacterencoding (encoding); } chain.dofilter (request, response); public void Destroy () {//TODO automatically generate method stub this.encoding = null; this.filterconfig = null;} Protected String selectencoding (ServletRequest request) {return (this.encoding);}}
|
and then Web.xml Plus
!--set Character encoding-->
set Character Encoding
com.struts.common.SetCharacterEncodingFilter
encoding
UTF-8
Set Character Encoding
/*
!--Set Character encoding-->
|
The benefits of using filters are many, especially among projects.
And more useful when using internationalization, as long as the page specifies <%@ page language= "java" pageencoding= "UTF-8"%>, the server will display the correct character set according to the local locale.
So I particularly recommend using filters.
method Three: Modify Tomcat's Server.xml file uriencoding
This approach is primarily for getting strings from URLs.
In tomcat5.0 and above versions, the Post and get methods differ in the processing of encodings. If you get Chinese in the URL, it will appear? Resolution However, there is no problem with the tomcat4.1 version, because the tomcat4.1 post and get methods are the same when coding.