From: http://www.javaeye.com/topic/483158
Application 1: solves the problem of Chinese garbled characters in Tomcat (a simple one first)
In tomcat, we usually solve the Chinese Garbled text problem as follows:
Filter code:
Java code
- Package filter;
- Import java. Io .*;
- Import javax. servlet .*;
- Import javax. servlet. http .*;
- Import wrapper. gethttpservletrequestwrapper;
- Public class contenttypefilter implements filter {
- Private string charset = "UTF-8 ";
- Private filterconfig config;
- Public void destroy (){
- System. Out. println (config. getfiltername () + "destroyed ");
- Charset = NULL;
- Config = NULL;
- }
- Public void dofilter (servletrequest request, servletresponse response,
- Filterchain chain) throws ioexception, servletexception {
- // Set the character encoding of the Request Response
- Request. setcharacterencoding (charset );
- Response. setcharacterencoding (charset );
- Httpservletrequest Req = (httpservletrequest) request;
- System. Out. println ("---- the request is" + config. getfiltername () + "filtered ");
- // Execute the next filter (if any, otherwise execute the target servlet)
- Chain. dofilter (req, response );
- System. Out. println ("---- the response is" + config. getfiltername () + "filtered ");
- }
- Public void Init (filterconfig config) throws servletexception {
- This. Config = config;
- String charset = config. getservletcontext (). getinitparameter ("charset ");
- If (charset! = NULL & charset. Trim (). Length ()! = 0)
- {
- This. charset = charset;
- }
- }
- }
Filter configuration in Web. xml:
XML Code
- <! -- Configure the character encoding to the application initialization parameter instead of the private initialization parameter of the filter because JSP and other places may also need to use -->
- <Context-param>
- <Param-Name> charset </param-Name>
- <Param-value> UTF-8 </param-value>
- </Context-param>
- <Filter>
- <Filter-Name> contenttypefilter </filter-Name>
- <Filter-class> filter. contenttypefilter </filter-class>
- </Filter>
- <Filter-mapping>
- <Filter-Name> contenttypefilter </filter-Name>
- <URL-pattern>/* </url-pattern>
- </Filter-mapping>
Request. setcharacterencoding (charset); It must be written before request. getparameter () is used for the first time, so that the parameter can be obtained according to the character encoding that has been set.
Response. setcharacterencoding (charset); It must be written before printwriter out = request. getwriter (). In this way, the out can be output according to the configured character encoding.
Through the filter, we can ensure that the character encoding of the request and response is set before the servlet or JSP execution.
However, this does not completely solve the Chinese Garbled text problem:
For post requests, neither the "get parameter link" nor the "output link" is correct;
For GET requests, there is no problem with the "output stage", but the "get parameters stage" still shows Chinese garbled characters, so the garbled characters are directly output during output.
The reason is that the location of the parameters stored in the post and get requests is different:
Post parameters are stored in the message body of the request data packet. The get method parameters are stored in the URI field of the request line of the request packet? Start attaching it to the URI field in the form of param = Value & parame2 = value2. Request. setcharacterencoding (charset); only applies to the data in the message body and does not take effect for parameters in the URI field. We usually use the following code to complete encoding conversion:
Java code
- String paramvalue = request. getparameter ("paramname ");
- Paramvalue = new string (paramvalue. Trim (). getbytes ("ISO-8859-1"), charset );
But every time such a conversion is really troublesome, is there a unified solution?
Solution 1: Set the uriencoding attribute to the appropriate character encoding in the connector element of tomcat_home \ conf \ Server. xml.
Java code
- <Connector Port = "8080" protocol = "HTTP/1.1"
- Connectiontimeout = "20000"
- Redirectport = "8443"
- Uriencoding = "UTF-8"
- />
The disadvantage is that other applications under the same Tomcat will also be affected. It is also difficult to modify the configuration of classes during each deployment.
Solution 2: Custom request wrapper wraps the request and adds the character encoding conversion to the getparameter () method.
Java code
- Package wrapper;
- Import java. Io. unsupportedencodingexception;
- Import java.net. urldecoder;
- Import javax. servlet. http. httpservletrequest;
- Import javax. servlet. http. httpservletrequestwrapper;
- Public class gethttpservletrequestwrapper extends httpservletrequestwrapper {
- Private string charset = "UTF-8 ";
- Public gethttpservletrequestwrapper (httpservletrequest request ){
- Super (request );
- }
- /**
- * Get the reference and character encoding of the decorated object
- * @ Param request
- * @ Param charset
- */
- Public gethttpservletrequestwrapper (httpservletrequest request,
- String charset ){
- Super (request );
- This. charset = charset;
- }
- /**
- * In fact, it is to call the getparameter method of the encapsulated request object to obtain the parameter, and then encode and convert it.
- */
- Public String getparameter (string name ){
- String value = super. getparameter (name );
- Value = NULL? Null: Convert (value );
- Return value;
- }
- Public String convert (string target ){
- System. Out. println ("Before encoding conversion:" + target );
- Try {
- Return new string (target. Trim (). getbytes ("ISO-8859-1"), charset );
- } Catch (unsupportedencodingexception e ){
- Return target;
- }
- }
- }
The code for modifying the dofilter method of the filter is as follows:
Java code
- Public void dofilter (servletrequest request, servletresponse response,
- Filterchain chain) throws ioexception, servletexception {
- // Set the character encoding of the Request Response
- Request. setcharacterencoding (charset );
- Response. setcharacterencoding (charset );
- // Newly added code
- Httpservletrequest Req = (httpservletrequest) request;
- If (req. getmethod (). inclusignorecase ("get "))
- {
- Req = new gethttpservletrequestwrapper (req, charset );
- }
- System. Out. println ("---- the request is" + config. getfiltername () + "filtered ");
- // Reference of the actual package object passed to the target servlet or JSP, instead of the original httpservletrequest object
- Chain. dofilter (req, response );
- System. Out. println ("---- the response is" + config. getfiltername () + "filtered ");
- }
In this way, the getparameters method of the package is called in the servlet to obtain the parameter, and the character encoding conversion process has been completed. We do not need to convert the character encoding every time we obtain the parameter.