When using Tomcat, I believe that we are all back to the problem of Chinese garbled, the specific performance of the form obtained by the Chinese data is garbled.
First, the primary solution through a search, many people have adopted the following method, firstly, to obtain a string according to iso8859-1 decoding conversion, and then follow the gb2312 encoding, and finally get the correct content.
The sample code is as follows:
http://xxx.do?ptname= ' I am Chinese '= Request.getparameter ("ptname"New String (Strptname.getbytes ("iso-8859-1"), "UTF-8"new string (Request.getparameter ("Para"). GetBytes ("Iso8859-1"), "gb2312");
This is due to the fact that Americans write Tomcat by default using ISO8859-1 encoding.
However, in our servlet and JSP pages There are a lot of parameters that need to be passed, so the conversion of the words will bring a lot of conversion code, very inconvenient.
Ii. Entry-level solutions later, we began to write a filter, before the client passed the parameters, through the filter will be the first to obtain the parameter encoding set to GB2312, and then you can directly use the getparameter to obtain the correct parameters. This filter has a detailed example of use in Tomcat's sample code Jsp-examples, where the filter is set in Web. XML, and the example uses the encoding of Japanese, as long as we modify it to gb2312 and view Plaincopy to Clipboa Rdprint? Set Character Encoding Filters. Setcharacterencodingfilter encoding EUC_JP Set Character encoding filters. Setcharacterencodingfilter encoding EUC_JP
The code for the filter is as follows:
View Plaincopy to Clipboardprint? Public classSetcharacterencodingfilterImplementsfilter{//the encoded stringprotectedString encoding =NULL;//configuration of the filterprotectedFilterconfig Filterconfig =NULL;//whether to ignore the encoding of the clientprotected BooleanIgnore =true;//Destroying Filters Public voiddestroy () { This. Encoding =NULL; This. Filterconfig =NULL; }//Filtration Method Public voidDoFilter (ServletRequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {//If you use a filter that ignores the client's encoding, use the filter to set the encodingif(Ignore | | (request.getcharacterencoding () = =NULL) ) {String encoding=selectencoding (request);if(Encoding! =NULL) request.setcharacterencoding (encoding); }//transfer to next filterChain.dofilter (request, response);}//Initialize Filter Public voidInit (Filterconfig filterconfig)throwsservletexception { 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 if(Value.equalsignorecase ("Yes")) This. Ignore =true;Else This. Ignore =false; }//returns the encoding of the filter settingsprotectedString selectencoding (ServletRequest request) {return( This. Encoding); } } Public classSetcharacterencodingfilterImplementsfilter{//the encoded stringprotectedString encoding =NULL;//configuration of the filterprotectedFilterconfig Filterconfig =NULL;//whether to ignore the encoding of the clientprotected BooleanIgnore =true;//Destroying Filters Public voiddestroy () { This. Encoding =NULL; This. Filterconfig =NULL; }//Filtration Method Public voidDoFilter (ServletRequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {//If you use a filter that ignores the client's encoding, use the filter to set the encodingif(Ignore | | (request.getcharacterencoding () = =NULL) ) {String encoding=selectencoding (request);if(Encoding! =NULL) request.setcharacterencoding (encoding); }//transfer to next filterChain.dofilter (request, response);}//Initialize Filter Public voidInit (Filterconfig filterconfig)throwsservletexception { 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 if(Value.equalsignorecase ("yes")) This. Ignore =true;Else This. Ignore =false; }//returns the encoding of the filter settingsprotectedString selectencoding (ServletRequest request) {return( This. encoding); } }
However, in the TOMCAT5, even if the use of filters, still can get garbled, why?
Iii. Advanced methods of Solution
This is because the processing of parameters in the TOMCAT4 and TOMCAT5 is not the same, in Tomcat4 the get is the same as the post encoding, so as long as the filter is set through the request.setcharacterencoding to solve the GE T with the post issue.
However, in Tomcat5, the get-to-post processing is performed separately in Tomcat 5, in order to solve the coding problem, the Tomcat author made a lot of efforts, specifically in the Tomcat configuration file Server.xml in the Connector element to increase the The following configuration parameters are added to directly configure the encoding uriencoding used to set the encoding used for content passed by the URI, and Tomcat will use the encoding specified here to encode the content delivered by the client.
What is a URI?
The description of the Java doc is as follows: The URI is a Uniform resource identifier, and the URL is a Uniform Resource locator.
Therefore, generally speaking, each URL is a URI, but not necessarily each URI is a URL. This is because the URI also includes a subclass, the Uniform Resource Name (URN), which names the resource but does not specify how to locate the resource. That is, the parameters we submit through the Get method are actually all submitted by URI, which is managed by this parameter, and if this parameter is not set, Tomcat will use the default iso8859-1 to encode the contents of the client. Usebodyencodingforuri uses the same code as the Body to handle the URI, this setting is to be compatible with the TOMCAT4, originally in the TOMCAT4 and TOMCAT5 Squadron parameter processing is not the same, in TOMCAT4 get and POS The encoding of T is the same, so you can solve the problem of Get and post by setting it once in the filter by request.setcharacterencoding.
However, in Tomcat5, get and post processing is performed separately, the get processing through the previous uriencoding processing, the post content is still through request.setcharacterencoding processing, in order to maintain compatibility , you have this setting. After you set Usebodyencodingforuri to True, you can solve the garbled problem in get and post directly by Request.setcharacterencoding. In this way, we can solve the problem of the parameter in the Get method by setting uriencoding in Server.xml, and use the filter to solve the problem in the Post method. Alternatively, you can solve the encoding problem by setting the Usebodyencodingforuri to true in Server.xml to match the filter. Here, I strongly suggest that during the creation of the site, the whole process using utf-8 coding to completely solve the garbled problem.
Here's how:
1. Page content is saved in utf-8 format and added to the page.
<mete http-equiv= "ContentType" content= "Textml;charst=utf-8" >
2, the server side of the Server.xml set Usebodyencodingforuri = True
3, using filter, filter set code for UTF-8
Four: If some transcoding can not be turned, but try to open Tomcat Server.xml, find and at the end add usebodyencodingforuri= "true" uriencoding= "UTF-8", as follows
<acceptcount= " connectiontimeout"= "20000" Disableuploadtimeout= "true" port= " redirectport"= "8443" usebodyencodingforuri= "true" uriencoding= "UTF-8">
Five: If you use Jstl, you can write your own El function, call Urlencoder.encode to encode.
ie defaults to the URL after the parameter is not encoded sent, but Tomat by default is to press iso8859-1 for URL decoding, so the above error occurs. The good practice is to:
1, in the URL parameters to ensure that the UTF-8 code, the method can be used with the JS function encodeURI (), or call the custom El function;
2, set Server.xml Connector familiar with uriencoding= "UTF-8", to ensure that the decoding format and encoding format are unified;
Method Four: View Plaincopy to Clipboardprint?
In action, String s=request.getparameter ("s"); S=new String (s.getbytes ("iso-8859-1"), "GBK");
Six: JS garbled solution
1. Client: Url=encodeuri (URL);
Server: String linename = new String (Request.getparameter ("name"). GetBytes ("Iso-8859-1"), "UTF-8");
2. Client: Url=encodeuri (encodeURI (URL)); Used 2 times encodeURI.
Server: String linename = request.getparameter (name); Java: Character decoding
Linename = Java.net.URLDecoder.decode (Linename, "UTF-8");
The solution of URL value in Chinese garbled