1. 程式人生 > 實用技巧 >使用HttpServletRequestWrapper重寫Request請求引數

使用HttpServletRequestWrapper重寫Request請求引數

目的:改變請求引數的值,滿足專案需求(如:過濾請求中 lang != zh 的請求)

1.使用 HttpServletRequestWrapper重寫

public class ChangeRequestWrapper extends HttpServletRequestWrapper {
    private Map<String, String[]> parameterMap; // 所有引數的Map集合

    public ChangeRequestWrapper(HttpServletRequest request) {
        super(request);
        parameterMap 
= request.getParameterMap(); } // 重寫幾個HttpServletRequestWrapper中的方法 /** * 獲取所有引數名 * * @return 返回所有引數名 */ @Override public Enumeration<String> getParameterNames() { Vector<String> vector = new Vector<String>(parameterMap.keySet());
return vector.elements(); } /** * 獲取指定引數名的值,如果有重複的引數名,則返回第一個的值 接收一般變數 ,如text型別 * * @param name 指定引數名 * @return 指定引數名的值 */ @Override public String getParameter(String name) { String[] results = parameterMap.get(name); return results[0]; }
/** * 獲取指定引數名的所有值的陣列,如:checkbox的所有資料 * 接收陣列變數 ,如checkobx型別 */ @Override public String[] getParameterValues(String name) { return parameterMap.get(name); } @Override public Map<String, String[]> getParameterMap() { return parameterMap; } public void setParameterMap(Map<String, String[]> parameterMap) { this.parameterMap = parameterMap; } }

2.新增Filter

public class LangFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        ChangeRequestWrapper changeRequestWrapper = new ChangeRequestWrapper((HttpServletRequest) servletRequest);

        Map<String, String[]> parameterMap = new HashMap<>(changeRequestWrapper.getParameterMap());

        String[] strings = parameterMap.get("lang");           //邏輯程式碼給定預設的引數值,如果引數不為中文,則直接返回異常
        if (strings == null || strings.length == 0) {
            strings = new String[1];
            strings[0] = "zh";
            parameterMap.put("lang", strings);
            changeRequestWrapper.setParameterMap(parameterMap);
        }else{
            String language = strings[0];
            if (!language.equals("zh")) {
                Map<String,String> error = new HashMap<>();
                error.put("code","500");

                ServletOutputStream outputStream = null;
                try {
                    outputStream = servletResponse.getOutputStream();
                    outputStream.write(JSONUtils.obj2Byte(error));
                } finally {
                    if (outputStream != null) {
                        outputStream.flush();
                        outputStream.close();
                    }
                }

                return;
            }
        }
        //使用複寫後的wrapper
        filterChain.doFilter(changeRequestWrapper, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

3.web.xml增加一個過濾器處理

<filter>
        <filter-name>languageFilter</filter-name>
        <filter-class>com.intercepor.LangFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>languageFilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>