During this time, I often see people asking how Chinese is always used in Web development? . The reason is actually very simple, because we mostly use Tomcat server, and Tomcat server default code for iso-8859-1 (Western European characters ). Because of the iso-8859-1 (Western European character) encoding that we often see? .
Method 1: the simplest and most commonly used method.
<% @ Page Language = "Java" pageencoding = "GBK" %>
Or <% @ page contenttype = "text/html; charset = GBK";> here, you can use gb2312 or GBK, which is only a string of many characters supported by GBK than gb2312.
This method is used to display Chinese characters on the JSP page.
Method 2: Use a filter.
Filters are mainly used for form submission. Are the data inserted into the database? . This is also for Tomcat not according to the request specified encoding, or self-made adopts the default encoding mode iso-8859-1 encoding.
Compile a setcharacterencodingfilter class.
Import java. Io. ioexception; Import javax. servlet. filter; Import javax. servlet. filterchain; Import javax. servlet. filterconfig; Import javax. servlet. servletexception; Import javax. servlet. servletrequest; Import javax. servlet. servletresponse; Public class setcharacterencodingfilter implements filter { Protected string encoding = NULL; Protected filterconfig = NULL; Protected Boolean ignore = true; Public void Init (filterconfig) throws servletexception { This. filterconfig = filterconfig; This. Encoding = filterconfig. getinitparameter ("encoding "); String value = filterconfig. getinitparameter ("Ignore "); If (value = NULL) This. Ignore = true; Else if (value. inclusignorecase ("true ")) This. Ignore = true; Else This. Ignore = false; } Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { // Todo automatically generates method stubs If (ignore | (request. getcharacterencoding () = NULL )){ String encoding = selectencoding (request ); If (encoding! = NULL) Request. setcharacterencoding (encoding ); } Chain. dofilter (request, response ); } Public void destroy (){ // Todo automatically generates method stubs This. Encoding = NULL; This. filterconfig = NULL; } Protected string selectencoding (servletrequest request ){ Return (this. Encoding ); } } |
Then, add web. xml
<! -- Set character encoding --> <Filter> <Filter-Name> set character encoding </filter-Name> <Filter-class> com. Struts. Common. setcharacterencodingfilter </filter-class> <Init-param> <Param-Name> encoding </param-Name> <Param-value> UTF-8 </param-value> </Init-param> </Filter><Filter-mapping> <Filter-Name> set character encoding </filter-Name> <URL-pattern>/* </url-pattern> </Filter-mapping> <! -- Set character encoding --> |
There are many benefits to using filters, especially in projects.
And it is more useful when using internationalization, as long as the page specifies <% @ page Language = "Java" pageencoding = "UTF-8" %>, the server displays the correct character set according to the local locale.
Therefore, we recommend that you use filters.
Method 3: Modify uriencoding In the Tomcat server. xml file.
<Connector DEBUG = "0" acceptcount = "100" connectiontimeout = "20000" disableuploadtimeout = "true" Port = "80" redirectport = "8443" enablelookups = "false" minsparethreads = "25" maxsparethreads = "75" Maxthreads = "150" maxpostsize = "0" uriencoding = "GBK"> </Connector> |
This method is mainly used to obtain strings from URLs.