请问下面代码不会进入无限循坏吗?

zzjj1988 3月前 ⋅ 390 阅读

目的:防止网站出现乱码,编写了一个过滤器EcondingFilter类来统一全站编码。我的疑问是:如果是以POST方式提交的数据,在代码里不会进入无限循坏吗?位置在代码里标记出来了。

public class EncodingFilter implements Filter {
    public void init(FilterConfig fileConfig) throws ServletException{

    }
    public void doFilter(ServletRequest request,ServletResponse response,
            FilterChain chain) throws IOException,ServletException {
        HttpServletRequest httpServletRequest=(HttpServletRequest) request;
        HttpServletRequest myRequest=new MyRequest(httpServletRequest);
        response.setContentType("text/html;charset=UTF-8");
        chain.doFilter(myRequest, response);
    }
    public void destroy() {

    }
}

class MyRequest extends HttpServletRequestWrapper{
    private HttpServletRequest request;
    private boolean hasEncode;
    public MyRequest (HttpServletRequest request) {
        super(request); 
        this.request=request;
    }

    @Override
    public Map getParameterMap() {
        String method=request.getMethod();
        if (method.equalsIgnoreCase("post")) {

            try {

                request.setCharacterEncoding("utf-8");
                
                
                //如果是POST的方式,在下面代码处不会进入无限循坏吗?
                return request.getParameterMap();
                
                
                
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }else if(method.equalsIgnoreCase("get")){
            Map<String, String[]>parameterMap=request.getParameterMap();
            if (!hasEncode) {			
                for(String parameterName:parameterMap.keySet()) {
                    String[] values=parameterMap.get(parameterName);
                    if (values!=null) {
                        for (int i = 0; i < values.length; i++) {
                            try {
                              
                                values[i]=new String(values[i].getBytes("ios-8859-01"),"utf-8");

                            } catch (UnsupportedEncodingException e) {
                                // TODO: handle exception
                                e.printStackTrace();
                            }
                        }
                    }
                }
                hasEncode=true;
            }
            return parameterMap;
        }
        return super.getParameterMap();
    }
    }
}

全部评论: 0

    我有话说: