Java工具类-设置字符编码

时间:2023-12-30 17:01:14
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
{ } }