GET乱码以及POST乱码的解决方法

一、GET乱码的解决方法

  在tomcat的server.xml文件中修下面的代码片段,在最后添加URIEncoding="UTF-8",修改后的结果如下

  Xml代码

  connectionTimeout="20000"

  redirectPort="8443" URIEncoding="UTF-8" />

  二、POST乱码解决方法

  自定义字符集过滤器,代码如下

  Java代码

  package com.filter;

  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 CharEncodingFilter implements Filter {

  private String charSet;

  public void init(FilterConfig config) throws ServletException {

  charSet = config.getInitParameter("encoding");

  if (charSet == null && charSet.length() < 1) {

  charSet = "UTF-8";

  }

  }

  public void doFilter(ServletRequest request, ServletResponse response,

  FilterChain filter) throws IOException, ServletException {

  request.setCharacterEncoding(this.getCharSet());

  response.setCharacterEncoding(this.getCharSet());

  filter.doFilter(request, response);

  }

  public void destroy() {

  this.setCharSet(null);

  }

  public void setCharSet(String charSet) {

  this.charSet = charSet;

  }

  public String getCharSet() {

  return (this.charSet);

  }

  }

  web.xml配置

  Xml代码

  CharEncodingFilter

  com.filter.CharEncodingFilter

  encoding

  UTF-8

你可能感兴趣的:(post)