处理服务器乱码的四种方式
在JSP文件中使用page命令指定响应结果的MIME类型,如<%@ page language="java" contentType="text/html;charset=utf-8" %>
2.在接受request参数时进行,response响应时设置编码格式
request.setCharacterEncoding(
"UTF-8"
)
response.setCharacterEncoding(
"UTF-8"
)
3.使用String设置字符集的方式处理乱码
indexString = java.net.URLDecoder.decode(indexString , "UTF-8");
indexString = new String(indexString.getBytes("UTF-8"), "UTF-8");
4.使用自定义过滤器
package com.cdu.yige.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;
import javax.servlet.http.HttpServletRequest;
public class EncodingFilter implements Filter {
private String charset = "UTF-8";
@Override
public void destroy() {}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
if(req.getMethod().equalsIgnoreCase("GET")) {
if(!(req instanceof GetRequest)) {
req = new GetRequest(req, charset);//处理get请求编码
}
} else {
req.setCharacterEncoding(charset);//处理post请求编码
}
chain.doFilter(req, response);
}
@Override
public void init(FilterConfig fConfig) throws ServletException {
String charset = fConfig.getInitParameter("charset");
if(charset != null && !charset.isEmpty()) {
this.charset = charset;
}
}
}
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";
@Override
public void destroy() {}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
if(req.getMethod().equalsIgnoreCase("GET")) {
if(!(req instanceof GetRequest)) {
req = new GetRequest(req, charset);//处理get请求编码
}
} else {
req.setCharacterEncoding(charset);//处理post请求编码
}
chain.doFilter(req, response);
}
@Override
public void init(FilterConfig fConfig) throws ServletException {
String charset = fConfig.getInitParameter("charset");
if(charset != null && !charset.isEmpty()) {
this.charset = charset;
}
}
}
对get方式的过滤
package com.cdu.yige.filter;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
* 对GET请求参数加以处理!
* @author lucien
*
*/
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;//如果为null,直接返回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;
// 遍历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;
}
}
import java.io.UnsupportedEncodingException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
* 对GET请求参数加以处理!
* @author lucien
*
*/
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;//如果为null,直接返回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;
// 遍历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;
}
}
过滤器web.xml文件配置
<
filter
>
<
filter-name
>
EncodingFilter
</
filter-name
>
<
filter-class
>
com.cdu.yige.filter.EncodingFilter
</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
EncodingFilter
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>
5.修改tomcat服务器server.xml的字符集配置,对get方式和post方式的乱码终极处理。
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
URIEncoding="UTF-8"
useBodyEncodingForURI="true"
redirectPort="8443" />
connectionTimeout="20000"
URIEncoding="UTF-8"
useBodyEncodingForURI="true"
redirectPort="8443" />
6.get请求方式和post请求方式产生的乱码处理方式略有不同,请自行百度参看。
其他方法参看: