前端传数据到servlet数据乱码
Posted max-hou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了前端传数据到servlet数据乱码相关的知识,希望对你有一定的参考价值。
使用过滤器
过滤器代码
package demo; 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.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; /** * Servlet Filter implementation class CharsetEncodingFilter */ @WebFilter(urlPatterns = {"/*"}, initParams = { @WebInitParam(name = "encoding", value = "UTF-8")}) public class CharsetEncodingFilter implements Filter { private FilterConfig config; public CharsetEncodingFilter() { } @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(config.getInitParameter("encoding")); chain.doFilter(request, response); } @Override public void init(FilterConfig fConfig) throws ServletException { this.config = fConfig; } }
以上是关于前端传数据到servlet数据乱码的主要内容,如果未能解决你的问题,请参考以下文章