Utf8 garbled solution [suitable for JSP applications deployed in Tomcat]

Source: Internet
Author: User

 

1. Java class: characterencodingfilter

 Import javax. servlet .*;

Import java. Io. ioexception;

 

Public class characterencodingfilter implements filter {

 

Private string encoding = "UTF-8 ";

Private Boolean forceencoding = true;

 

/**

* Set the encoding to use for requests. This encoding will be

* Passed into a servletrequest. setcharacterencoding call.

* <P> whether this encoding will override existing request

* Encodings depends on the "forceencoding" flag.

*/

Public void setencoding (string encoding ){

This. Encoding = encoding;

}

 

/**

* Set whether the encoding of this filter shocould override existing

* Request encodings. Default is "false", I. e. Do not modify Encoding

* If servletrequest. getcharacterencoding returns a non-null value.

*/

Public void setforceencoding (Boolean forceencoding ){

This. forceencoding = forceencoding;

}

 

Public void Init (filterconfig) throws servletexception {

This. Encoding = filterconfig. getinitparameter ("encoding ");

String force = filterconfig. getinitparameter ("forceencoding ");

This. forceencoding = (force = NULL) | Boolean. valueof (force). booleanvalue ();

}

 

Public void dofilter (servletrequest,

Servletresponse,

Filterchain) throws ioexception, servletexception {

If (this. forceencoding | servletrequest. getcharacterencoding () = NULL ){

Servletrequest. setcharacterencoding (this. Encoding );

}

Filterchain. dofilter (servletrequest, servletresponse );

}

 

Public void destroy (){

 

}

 

}

 

2. Add filters configurations to the Web. xml configuration file.

 <! -- Filters configurations -->

<Filter>

<Filter-Name> character_encoding </filter-Name>

<Filter-class>

Common. encoding. characterencodingfilter </filter-class>

<Init-param>

<Param-Name> encoding </param-Name>

<Param-value> UTF-8 </param-value>

</Init-param>

<Init-param>

<Param-Name> forceencoding </param-Name>

<Param-value> true </param-value>

</Init-param>

</Filter>

<Filter-mapping>

<Filter-Name> character_encoding </filter-Name>

<URL-pattern>/* </url-pattern>

</Filter-mapping>

 

3. The database is created in UTF-8 format
4. Right-click Project Properties in UTF-8 format
5. All page declarations as UTF-8
6. jdbc url is set to: UTF-8

JDBC: mysql: // localhost: 3306/databaseuse? Useunicode = true & characterencoding = UTF-8
7. Database driver select UTF-8 format
8. Tomcat encoding into UTF-8

Add uriencoding = "UTF-8" in server. xml"

<Connector Port = "8080" maxhttpheadersize = "8192" maxthreads = "150" minsparethreads = "25" maxsparethreads = "75" enablelookups = "false" redirectport = "8443" acceptcount =" 100 "connectiontimeout =" 20000 "disableuploadtimeout =" true "uriencoding =" UTF-8 "/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.