java 代码
java中文处理:
一,在tomcat的server.xml文件中加入
java 代码
- <Connector
- ="8080" maxHttpHeaderSize="8192"
- maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
- enableLookups="false" redirectPort="8443" acceptCount="100"
- connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK"/>
URIEncoding="GBK"部分
二,做一个过滤器:
java 代码
- package weijin.com.bbs.system.filter;
-
- 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 EncodingFilter implements Filter
- {
-
- String defaultEncoding = "utf8";
-
- public void init(FilterConfig config) throws ServletException
- {
- String encoding = config.getInitParameter("encoding");
- if (encoding != null)
- {
- defaultEncoding = encoding;
- }
- }
-
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
- {
- request.setCharacterEncoding(defaultEncoding);
- chain.doFilter(request, response);
- }
-
- public void destroy()
- {
- defaultEncoding = null;
- }
-
-
- }
三:在web.xml中配置:
java 代码
- <filter>
- <filter-name>Encoding</filter-name>
- <filter-class>weijin.com.bbs.system.filter.EncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
-
- <filter-mapping>
- <filter-name>Encoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
<filter-name></filter-name>
OK,完成!