码迷,mamicode.com
首页 > 编程语言 > 详细

Java工具类-设置字符编码

时间:2018-11-06 22:36:33      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:str   class   throws   odi   chain   cte   import   数据库   filter   

package common;

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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *    设置字符编码的类,目的是将页面上传过来的数据统一编码为UTF-8,这样做是为了避免当数据插入数据库后会中文乱码问题 
*/
public class EncodeFilter implements Filter
{

    public void destroy()
    {
        // TODO Auto-generated method stub

    }

    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain chain) throws IOException, ServletException
    {
        HttpServletRequest request = (HttpServletRequest) arg0;
        HttpServletResponse response = (HttpServletResponse) arg1;
        
        request.setCharacterEncoding("utf-8");
        
        chain.doFilter(request, response);
    }

    public void init(FilterConfig arg0) throws ServletException
    {
        
    }

}

 

Java工具类-设置字符编码

标签:str   class   throws   odi   chain   cte   import   数据库   filter   

原文地址:https://www.cnblogs.com/Roni-i/p/9918068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!