Servlet中文乱码处理
一、表单提交:
1.客户端的get请求
2.客户端post请求
request. setCharacterEncoding(“UTF-8”);
运用过滤器均可以解决
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;
public class EncodingFilter implements Filter {
private String charset = "UTF-8";
public EncodingFilter() {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
Object req = (HttpServletRequest)request;
if(((HttpServletRequest)req).getMethod().equalsIgnoreCase("GET")) {
if(!(req instanceof GetRequest)) {
req = new GetRequest((HttpServletRequest)req, this.charset);
}
} else {
((HttpServletRequest)req).setCharacterEncoding(this.charset);
}
chain.doFilter((ServletRequest)req, response);
}
public void init(FilterConfig fConfig) throws ServletException {
String charset = fConfig.getInitParameter("charset");
if(charset != null && !charset.isEmpty()) {
this.charset = charset;
}
}
}
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
* 对GET请求参数加以处理!
* @author qdmmy6
*
*/
public class GetRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
private String charset;
public GetRequest(HttpServletRequest request, String charset) {
super(request);
this.request = request;
this.charset = charset;
}
@Override
public String getParameter(String name) {
String value = request.getParameter(name);
if(value == null) return null;
try {
return new String(value.getBytes("ISO-8859-1"), charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Map getParameterMap() {
Map<String,String[]> map = request.getParameterMap();
if(map == null) return map;
for(String key : map.keySet()) {
String[] values = map.get(key);
for(int i = 0; i < values.length; i++) {
try {
values[i] = new String(values[i].getBytes("ISO-8859-1"), charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
return map;
}
@Override
public String[] getParameterValues(String name) {
String[] values = super.getParameterValues(name);
for(int i = 0; i < values.length; i++) {
try {
values[i] = new String(values[i].getBytes("ISO-8859-1"), charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
return values;
}
}
二、url编码
![服务器编码](https://img-blog.csdn.net/20160817171938528)
1. jsp页面可以使用jstl中的C标签
例如:
<c:url value="BookServlet" var="authorUrl">
<c:param name="method" value="findByAuthor"/>
<c:param name="author" value="${book.author }"/>
</c:url>
2.修改Tomcat的编码
常用Tomcat作为运行Servlet,而Tomcat内部默认的编码是ISO-8859-1,所以对于get请求方式,其传递的数据(URI)会附加在访问的资源后面,其编码是Tomcat默认的,如果修改该URI的编码,那么对于所有的get请求方式便不会出现乱码了包括上边说的重定向和超链接,在Tomcat的配置文件server.xml中找到修改Tomcat的端口的地方,在其内部加入URIEncoding属性,设置为和你的项目中所设的编码一样的值,这里全部都是UTF-8。如下所示:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"redirectPort="8443" URIEncoding="UTF-8" />
3.浏览器较低版本可使用
进行编码:URLEncoder.encode(stuname,”UTF-8”)
进行解码:URLDecoder.decode(stuname,”UTF-8”)
三、回显错误(若传来没乱码)
response.setContentType(“text/html;charset=utf-8”);
response. setCharacterEncoding(“UTF-8”);
四、数据库乱码
有时会用JDBC连接MySQL数据库,可能会出现乱码,所以要在连接之后加上如下的语句:
useUnicode=true&characterEncoding=UTF-8
如果使用数据源:
以下是c3p0的配置文件里设置:
jdbc:mysql://localhost:3306/subway?useUnicode=true&characterEncoding=UTF8