一:使用过滤器解决网get/post请求的乱码
1:过滤器
package com.chinacache.utils;
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;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
//解决get和post请求的编码问题
public class CharacterFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;//Tomcat
HttpServletResponse response = (HttpServletResponse)resp;//Tomcat
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
MyHttpServletRequest mrequest = new MyHttpServletRequest(request);
chain.doFilter(mrequest, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
//1、实现与被增强对象共同的接口(或继承被增强对象)
//2、定义被增强对象的引用
//3、通过构造方法接收被增强对象
//4、修改需要被增强的方法
//5、不需要增强的方法,调用原对象的对应方法
class MyHttpServletRequest extends HttpServletRequestWrapper{
private HttpServletRequest request;
public MyHttpServletRequest(HttpServletRequest request){
super(request);
this.request = request;
}
public String getParameter(String name) {
try{
String value = request.getParameter(name);
if(value==null)
return value;
//get方式才需要增强
String method = request.getMethod();//请求方式:get post
if("get".equalsIgnoreCase(method)){
value = new String(value.getBytes("ISO-8859-1"),request.getCharacterEncoding());
}
return value;
}catch(Exception e){
throw new RuntimeException(e);
}
}
}
2:web.xml
<filter> <filter-name>CharacterFilter</filter-name> <filter-class>com.chinacache.utils.CharacterFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
二:如果使用spring mvc 的注解@ResponseBody 直接返回中文字符串会出现 "??????" 乱码
解决:
方法1:使用ajax请求时设置返回类型,返回json,xml可以正常显示中文
$.ajax({
type:'get',//可选get
url:'${projectPath}/execrevokesucceedrecord.json', //.json .xml
data:{"id":id} ,
dataType:'text',//服务器返回的数据类型 可选XML ,Json jsonp script htmltext等
success:function(msg){
},
error:function(){
}
})
type:'get',//可选get
url:'${projectPath}/execrevokesucceedrecord.json', //.json .xml
data:{"id":id} ,
dataType:'text',//服务器返回的数据类型 可选XML ,Json jsonp script htmltext等
success:function(msg){
},
error:function(){
}
})
方法2:不返回string
参考:http://blog.csdn.net/hurryjiang/article/details/7710110
二:img 标签的 src 中存在乱码(http://localhost:8081/starscream/resources/images/bigUser/type_2__%E6%99%BA%E8%83%BD%E4%B8%8B%E8%BD%BD%E6%9C%8D%E5%8A%A1.jpg)
过滤器设置(我的图片在resources目录下)
if (hr.getRequestURI().contains("resources")) {
String uri = hr.getRequestURI();
String ch = URLDecoder.decode(uri, "UTF-8");
if (uri.equals(ch)) {
chain.doFilter(request, response);
} else {
ch = ch.substring(hr.getContextPath().length());
config.getServletContext().getRequestDispatcher(ch).forward(request, response);
}
}
js页面中的使用:url=encodeURI(url);
参拷(http://bbs.csdn.net/topics/290057574)
package com.test;
import java.io.IOException;
import java.net.URLDecoder;
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 UrlFilter implements Filter {
public final static String DEFAULT_URI_ENCODE = "UTF-8";
private FilterConfig config = null;
private String encode = null;
@Override
public void init(FilterConfig config) throws ServletException {
this.config = config;
this.encode = config.getInitParameter("DEFAULT_URI_ENCODE");
if(this.encode == null) {
this.encode = DEFAULT_URI_ENCODE;
}
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
String uri = request.getRequestURI();
String ch = URLDecoder.decode(uri, encode);
if(uri.equals(ch)) {
chain.doFilter(req, res);
return;
}
ch = ch.substring(request.getContextPath().length());
config.getServletContext().getRequestDispatcher(ch).forward(req, res);
}
@Override
public void destroy() {
config = null;
}
}
web.xml
<filter> <filter-name>urlFilter</filter-name> <filter-class>com.test.UrlFilter</filter-class> <init-param> <param-name>DEFAULT_URI_ENCODE</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>urlFilter</filter-name> <url-pattern>/*.jpg</url-pattern> </filter-mapping>