web乱码问题
String data = "不见青山"; ServletOutputStream sos = response.getOutputStream(); byte b[] = data.getBytes(); //默认用平台的gbk编码 sos.write(b) ------------------------------------------------------------------------ (1) response.setHeader("Content-Type", "text/html;charset=UTF-8"); (2) response.setContentType("text/html;charset=UTF-8"); String data = "不见青山"; ServletOutputStream sos = response.getOutputStream(); byte b[] = data.getBytes("UTF-8"); //默认用平台的gbk编码 sos.write(b) 字符 (1) response.setCharacterEncoding("UTF-8"); (2) response.setContentType("text/html;charset=UTF-8"); String data = "春天里"; PrintWriter pw = rsponse.getWriter(); pw.write(data)//默认用ISO-8859-1
以上为response响应乱码解决问题
提交乱码
web配置全局过滤器
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 CodeFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); chain.doFilter(request, response); } public void destroy() { } }
<filter> <filter-name>CodeFilter</filter-name> <filter-class>com.springMVC.filter.CodeFilter</filter-class> </filter> <filter-mapping> <filter-name>CodeFilter</filter-name> <url-pattern>*</url-pattern> </filter-mapping>