zoukankan      html  css  js  c++  java
  • request对象添加额外的参数

    一、重写 HttpServletRequestWrapper
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Vector;
    
    /**
     * <p>向filter过滤器中的request对象添加额外的参数</p><br>
     * TODO(重写 HttpServletRequestWrapper)
     *
     * @author han.sun
     * @version 1.0.0
     * @since 2019/9/16 09:22
     */
    public class RequestParameterWrapper extends HttpServletRequestWrapper {
    
    
        private Map<String, String[]> params = new HashMap<>();
    
        /**
         * override构造器
         *
         * @param request     HttpServletRequest
         * @param extraParams 自定义参数Map
         */
        RequestParameterWrapper(HttpServletRequest request, Map<String, Object> extraParams) {
            this(request);
            addParameters(extraParams);
        }
    
        /**
         * 重载构造函数
         *
         * @param request HttpServletRequest
         */
        private RequestParameterWrapper(HttpServletRequest request) {
            super(request);
            //将现有parameter传递给params
            this.params.putAll(request.getParameterMap());
        }
    
        /**
         * 获取所有参数名
         *
         * @return all parameter names
         */
        @Override
        public Enumeration<String> getParameterNames() {
            Vector<String> vector = new Vector<>(params.keySet());
            return vector.elements();
        }
    
    
        @Override
        public Map<String, String[]> getParameterMap() {
            return params;
        }
    
    
        /**
         * 添加参数
         *
         * @param extraParams 自定义参数Map
         */
        private void addParameters(Map<String, Object> extraParams) {
            for (Map.Entry<String, Object> entry : extraParams.entrySet()) {
                addParameter(entry.getKey(), entry.getValue());
            }
        }
    
    
        /**
         * 添加参数
         *
         * @param name  key
         * @param value value
         */
        private void addParameter(String name, Object value) {
            if (value != null) {
                if (value instanceof String[]) {
                    params.put(name, (String[]) value);
                } else if (value instanceof String) {
                    params.put(name, new String[]{(String) value});
                } else {
                    params.put(name, new String[]{String.valueOf(value)});
                }
            }
        }
    
        /**
         * 重写getParameter,参数从当前类中的map获取
         *
         * @param name key
         * @return value
         */
        @Override
        public String getParameter(String name) {
            String[] values = params.get(name);
            if (values == null || values.length == 0) {
                return null;
            }
            return values[0];
        }
    
        /**
         * 重写getParameter,参数从当前类中的map获取
         *
         * @param name key
         * @return value
         */
        @Override
        public String[] getParameterValues(String name) {
            return params.get(name);
        }
    
    
    }

    2、编写web请求参数过滤器

    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * <p>filter---参数过滤器</p><br>
     * <p>请求已经有该参数,(过滤器覆盖value是无效的)</p><br>
     * TODO(设置自定义request参数)
     *
     * @author han.sun
     * @version 1.0.0
     * @since 2019/5/5 13:12
     */
    public class RequestParameterFilter implements Filter {
    
        @Override
        public void init(FilterConfig filterConfig) {
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            String msgId = request.getParameter("msgId");
            if (msgId == null) {
                Map<String, Object> map = new HashMap<String, Object>(16);
                map.put("msgId", new String[]{System.currentTimeMillis() + ""});
                RequestParameterWrapper requestParameterWrapper = new RequestParameterWrapper(request, map);
                chain.doFilter(requestParameterWrapper, servletResponse);
            }else {
                chain.doFilter(servletRequest, servletResponse);
            }
        }
    
        @Override
        public void destroy() {
        }
    }

    3、在web.xml中配置过滤规则

        <!-- 自定义过滤器 -->
        <filter>
            <filter-name>requestParameterFilter</filter-name>
            <filter-class>com.eastrobot.log.web.filter.RequestParameterFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>requestParameterFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
  • 相关阅读:
    javascript中的常用表单事件用法
    关于js键盘事件的例子
    对象间引用赋值及方法时引用传递
    反编译工具reflector破解方法
    使用委托(C# 编程指南)
    委托(C# 编程指南)
    浅谈线程池(下):相关试验及注意事项
    Lambda 表达式(C# 编程指南)
    浅谈线程池(中):独立线程池的作用及IO线程池
    浅谈线程池(上):线程池的作用及CLR线程池
  • 原文地址:https://www.cnblogs.com/han-sun/p/10845689.html
Copyright © 2011-2022 走看看