项目中文乱码
先配置自定义VM
设置编码
-Dfile.encoding=utf-8
配置虚拟机参数 -Dfile.encoding=utf-8
创建一个过滤器
只需要在项目的src下web目录中创建一个filter包建ChineseFilter.java类写一下代码即可
@WebFilter(urlPatterns = "/*")
public class ChineseFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
filterChain.doFilter(request,response);
}
@Override
public void destroy() {
}
}