1、Mysql
【注意】client和mysqld的字符集都要修改;windows下为my.ini文件,linux下为my.cnf文件
2、连接mysql的url
3、filter
步骤1: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 CharsetEncodingFilter implements Filter {
- private String encoding;
- public void destroy() {
- }
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
- FilterChain filterChain) throws IOException, ServletException {
- //设置字符集
- servletRequest.setCharacterEncoding(encoding);
- filterChain.doFilter(servletRequest, servletResponse);
- }
- public void init(FilterConfig filterConfig) throws ServletException {
- //取得初始化参数
- this.encoding = filterConfig.getInitParameter("encoding");
- }
- }
步骤2.在web.xml配置字符编码
- <filter>
- <filter-name>CharsetEncodingFilter</filter-name>
- <filter-class>com.lx.filter.CharsetEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>GBK</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharsetEncodingFilter</filter-name>
- <url-pattern>*.jsp</url-pattern> <!--只对提交到的jsp页面起作用-->
- </filter-mapping>
4、jsp
<%@ page contentType= "text/html;charset=gbk" %>
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/682279,如需转载请自行联系原作者