Before the use of filters, we solve the garbled method is at the front of each servlet to add
Response garbled:
response.setCharacterEncoding("utf-8");//通知服务器 response.setContentType("text/html;charset=utf-8"//通知浏览器(其实底层也通知了服务器 所以上一行代码其实可用不用写)
The POST request is garbled:
request.setCharacterEncoding(“utf-8”);//jsp的pageEncoding为 utf-8的前提下//通知服务器 以浏览器一样的编码方法打开
GET request garbled:
For get commit garbled, can only manually encode and decode to solve garbled problem: (because the server default is Iso8859-1, so first use it to decode to binary, and then use Utf-8 code back)
StringrequestnewString(username.getBytes(“iso8859-1”),”utf-8
Each servlet writes, too laborious, and then we use filters to solve this problem:
(in order to solve the get, we need to decode it with iso8859-1, then encode it with U8, and then re-upload it to the request, but!) Request only gets the method of requesting parameters, without the method of set parameters, all we have to rewrite the method to get the request parameters, we can take the inheritance, decorator mode or dynamic proxy, but the inheritance in this is not very applicable, because the request this object has been born, This time we use the decoration (first write a class implementation and the same as the decorator's interface, and then provide him with a construction method to be the decorator passed in, for the method of modification to rewrite, for the method that do not want to change the original method can be called))
1. Write a class implementation HttpServletRequest interface, but this interface method too many, and most do not need us to rewrite, so this does not apply, here we inherit httpservletrequestwrapper on the line, He has rewritten all the methods of httpservletrequest, and we just have to inherit him and rewrite Getparameter,getparametervalues,getparametermap.
Public class encodingfilter implements Filter { PrivateFilterconfig config =NULL;PrivateString encode =NULL; Public void Destroy() { } Public void DoFilter(ServletRequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {response.setcontenttype ("text/html;charset="+ encode);//--Solve garbled responseChain.dofilter (NewMyhttpservletrequest ((httpservletrequest) request), response);//--Packaging retrofit request and get request parameter related methods to resolve requests parameter garbled} Public void Init(Filterconfig filterconfig)throwsservletexception { This. config = filterconfig; Encode = Config.getinitparameter ("Encode") ==NULL?"Utf-8": Config. getinitparameter ("Encode"); } class Myhttpservletrequest extends Httpservletrequestwrapper {PrivateHttpServletRequest request =NULL;Private BooleanIsnotencode =true;//When you first come in, you organize the map, and for GET requests, use the Public myhttpservletrequest(HttpServletRequest request) {Super(request); This. request = Request; }@Override PublicMapGetparametermap() {Try{if(Request.getmethod (). Equalsignorecase ("POST")) {//--if the post is submitted, one line of code resolves the post submission request parameter garbledRequest.setcharacterencoding (encode);returnRequest.getparametermap (); }Else if(Request.getmethod (). Equalsignorecase ("GET")) {//--if it is a get commit, it should be manually encoded to solve garbledmap<string, string[]> map = Request.getparametermap ();//Get a garbled map if(Isnotencode) {//Only in the first time to solve garbled for(Map.entry<string, String[]> entry:map.entrySet ()) {//Traverse map To resolve all values garbledString[] vs = Entry.getvalue (); for(inti =0; i < vs.length; i++) {Vs[i] =NewString (Vs[i].getbytes ("Iso8859-1"), encode); }} Isnotencode =false;//Set to False, the second time will not be in this code block again}returnMap }Else{returnRequest.getparametermap (); } }Catch(Exception e) {E.printstacktrace ();Throw NewRuntimeException (e); } }@Override PublicString[]getparametervalues(String name) {return(string[]) Getparametermap (). get (name); }@Override PublicStringGetParameter(String name) {returnGetparametervalues (name) = =NULL?NULL: getparametervalues (name) [0]; } } }
The above GET request to be aware that when the first time to get the parameters to solve the garbled map will be cached, call Getparametxxx again will use this map, because after the first time after the map data is already utf-8, At this time again with iso8859-1 decoding is again garbled. So, we have to use a variable control, get garbled to solve the code is executed only once.
Re-learning Javaweb---Filter application--the whole station garbled