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 filterConfig = null;
Protected boolean ignore = true;
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 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.
In tomcat5.0 and later versions, the post and get methods are different in encoding. If you get Chinese characters in the URL, what will happen? . But there is no problem in tomcat4.1, because the post and get methods of tomcat4.1 are the same in encoding.