Here is a specific example:
Now because the browser supports UTF-8 support, we can easily process the target of international and character encoding issues by using Unicode encoding in the source file, request, and response. Take the Tomcat4.1.2 we use as an example, the process is as follows:
1. When writing a JSP page: In each JSP page, you should add a line in the page: <% @ page contenttype = "text / html; charset = uTF-8" Language = "java"%> When editing the JSP page, To ensure that the JSP file is saved in Unicode, almost all editors are available in Unicode encoding or convert the file content to Unicode.
2, add a class setcharacterencodingfilter.java used to declare the Request's characterEncodingFilter.java; setcharacterencodingfilter's main role is: Encoding declarations when submitted from the page to the Server side, as our desirable eNCoding, by calling Request Method setCharacterencoding (String Encoding) Changes, which allows REQUEST from the client, pressing the encoded data encoded in Web.xml (in the second point to say).
3, modify the web.xml file, configure a filter to filter all URL requests, declare that all URL requests are not UTF-8 in the second step in the second step. Add the following paragraphs in the web.xml file:
Set Character Encoding
Org.kyle.web.sample.setcharacterencodingfilter
ENCODING
UTF-8
Set Character Encoding
/ *
In the above text, "Org.Kyle.Web.Sample.SetCharacterenCodingFilter" Specifies the location of the class in step 2, "UTF-8" specifies the request to declare the request, "/ *" specifies this Filter application. The range (here is all URL requests).
At the same time, pay attention to the two questions: 1: The version of the servlet must be supported by Request.SetCharacterencoding (String Encoding). That is, it is above Servert 2.3. 2: The current code page attribute setting of the control panel area must be set to "936 (GBK)", if it is "437 (OEM-United State", it is 8-bit when it is handled, and Chinese and Japanese are 16 -BIT. So when it is displayed and processed, it cuts the first 8 bits of Chinese, which will appear garbled.
Annex: SetCharacterEncodingFilter source package org.kyle.web.sample; 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; import javax.servlet.UnavailableException; public class SetCharacterEncodingFilter implements filter {/ ** * The default character encoding to set for requests that pass through * this filter * / protected String encoding. = NULL; / ** * The Filter Configuration Object We Are Associated with. IF this value * is null, this filter instance is not currently configured. * / protected filterconfig filterfig = null;
? / ** * Should a character encoding specified by the client be ignored * / protected boolean ignore = true;. / ** * Take this filter out of service * / public void destroy () {this.encoding = null; this. filterConfig = null;} / ** * Select and set (if specified) the character encoding to be used to * interpret request parameters for this request * * @param request The servlet request we are processing * @param result The servlet response we. are creating * @param chain The filter chain we are processing * * @exception IOException if an input / output error occurs * @exception ServletException if a servlet error occurs * / public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException , Servletexception {// conditionally select and set the character encoding to be used if (ignore || (Request.getCharacterencoding () == null)) {String encoding = selectEncoding (request); if (encoding = null) request.setCharacterEncoding (encoding);!} // Pass control on to the next filter chain.doFilter (request, response);} / ** * Place this filter inTo service. * * @Param filterconfig the filter configuration object **
ENCODING
*
UTF-8
*
* / Public void init (FilterConfig filterConfig) throws ServletException {this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter ( "encoding"); String value = filterConfig.getInitParameter ( "ignore"); if (value == null) This.Ignore = true; else.equalsignorecase ("true")) this.Ignore = true; Else IF (Value.Equalsignorecase ("YES") this.Ignore = true; else this.ignore = false;} / ** * Select an appropriate character encoding to be used, based on the * characteristics of the current request and / or filter initialization * parameters. If no character encoding should be set, return * null. ** The default implementation unconditionally returns the Value configured * by The Encoding Initialization Parameter for this * filter. * * @Param Request the servlet request we are processing * / protected String SelectEncoding ServletRequest Request) {Return (this.encoding);}}