Java WEB开发乱码问题
一.Servlet乱码问题的解决
利用filter和request.getParamter()方法即可解决,直接上代码
1.CodeFilter:过滤器拦截所有请求
public class CodeFilter implements Filter{
@Override
public void destroy() {}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request =(HttpServletRequest) req;
String method =request.getMethod().toLowerCase();
if("post".equals(method)){
request.setCharacterEncoding("UTF-8");
}else{
request=new MyHttpservletrequest(request);
}
chain.doFilter(request, resp);
}
@Override
public void init(FilterConfig arg0) throws ServletException {}
}
2.MyHttpservletrequest:重写getParamter()方法
public class MyHttpservletrequest extends HttpServletRequestWrapper{
public MyHttpservletrequest(HttpServletRequest request) {
super(request);
}
@Override
public String getParameter(String name) {
String value=name;
if(value!=null){
try {
value =new String(super.getParameter(value).getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return value;
}
}
二.struts2解决乱码问题
与servlet解决乱码类似,只是struts2通过getParamterMap()获取参数,所以要重写getParamterMap()方法
1.EncodingFilter
//继承Struts默认的过滤器:StrutsPrepareAndExecuteFilter
public class EncodingFilter extends StrutsPrepareAndExecuteFilter {
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
if(request.getMethod().equalsIgnoreCase("get")){
request = new MyHttpRequest(request);
}
request.setCharacterEncoding("UTF-8");
super.doFilter(request, resp, chain);
}
}
2.重写getparamterMap()的request:MyHttpRequest
public class MyHttpRequest extends HttpServletRequestWrapper {
public MyHttpRequest(HttpServletRequest request) {
super(request);
}
//struts内部使用getparameterMap()获取请求参数
/**
* 重写getParameterMap()方法
*/
@Override
public Map getParameterMap() {
Map map = super.getParameterMap();
Map nMap = new HashMap();
String[] value = null;
for (String key : map.keySet()) {
value = new String[map.get(key).length];
for (int i = 0; i < map.get(key).length; i++) {
try {
value[i] = new String(
map.get(key)[i].getBytes("ISO-8859-1"), "utf-8");
nMap.put(key, value);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
return nMap;
}
}
三.SpringMVC解决乱码:
SpringMVC通过getParamterValues()获取参数,所以要重写getParamterValues()方法
1.MyEncodingFilter
public class MyEncodingFilter extends CharacterEncodingFilter{
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if(request.getMethod().equalsIgnoreCase("post")){
super.setEncoding("utf-8");
}else if(request.getMethod().toLowerCase().equalsIgnoreCase("get")){
request = new MyHttpRequest(request);
}
super.doFilterInternal(request, response, filterChain);
}
}
2.MyHttpRequest :负责重写getPramterValues()方法
//springMVC 获取前台参数用:getParamterValues
public class MyHttpRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
public MyHttpRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
@Override
public String[] getParameterValues(String name) {
String value = name;
String[] reqestValue = new String[] { "" };
if (value == null) {
return super.getParameterValues(value);
} else {
reqestValue = request.getParameterValues(name);
for (int i = 0; i < reqestValue.length; i++) {
try {
String str = new String(
reqestValue[i].getBytes("iso8859-1"), "utf-8");
reqestValue[i] = str;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return reqestValue;
}
}
}
以上就是Java web开发常遇到的乱码的问题的解决方案,不对的地方请大家指正