The first thing to make clear is that the server here refers to Tomcat.
In the case where the page does not explicitly specify the encoding, the client passes two values param1 and param2 to the server via the input tag and string. If you use the Request.getparameter () method directly to get the value, the resulting is definitely garbled, we need to re-encode it, as shown in the following code:
New String (Req.getparameter ("param1"). GetBytes ("Iso-8859-1"), "GBK");
This is because, in the Chinese environment, the browser will be GBK to the data passed to the server encoding, and the server side will default to Iso-8859-1 decoding, if we do not do the corresponding processing, the resulting will be garbled.
It would be cumbersome to encode and decode every time the client's data is fetched, but fortunately there is a good workaround. We can write a filter that tells the server what the encoding of the request is before it reaches the servlet. A simple example is as follows:
PackageCom.thief.filter;Importjava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse; Public classMyfilterImplementsFilter {@Override Public voidInit (filterconfig config)throwsservletexception {} @Override Public voidDoFilter (servletrequest request, servletresponse response, Filterchain arg2)throwsIOException, servletexception {request.setcharacterencoding ("GBK"); Arg2.dofilter (request, response); } @Override Public voiddestroy () {}}
At this time, the data can be obtained directly from the servlet via Request.getparameter (), but this is limited to the data passed by the input tag, and the query string is passed in accordance with the encoding and decoding process described above.
At this point, there are two solutions: one is to declare usebodyencodingforuri= "true" under Connector in Tomcat's server configuration file, and the second is to configure it under connector element Uriencoding to set the URL encoding format, if not set by default is Iso-8859-1.
The two scenarios I checked under the Startup.bat command-line window under Tomcat's bin are correct. But the Eclipse console does not see the effect, this does not know what the reason.
Reference: Http://www.tuicool.com/articles/IbaQza
Server-side Get form data encoding and decoding problem (servlet)