zoukankan      html  css  js  c++  java
  • 更改Request Parameters中的值

    1. 定义ParameterRequestWrapper 继承HttpServletRequestWrapper

    public class ParameterRequestWrapper extends HttpServletRequestWrapper {
    
        private Map<String,String[]> params;
    
        public ParameterRequestWrapper(HttpServletRequest request, Map<String,String[]> newParams) {
            super(request);
            this.params = newParams;
        }
    
        @Override
        public String getParameter(String name) {
            Object v = params.get(name);
            if (v == null) {
                return null;
            } else if (v instanceof String[]) {
                String[] strArr = (String[]) v;
                if (strArr.length > 0) {
                    return strArr[0];
                } else {
                    return null;
                }
            } else if (v instanceof String) {
                return (String) v;
            } else {
                return v.toString();
            }
        }
    
        @Override
        public Map getParameterMap() {
            return params;
        }
    
        @Override
        public Enumeration getParameterNames() {
            return new Vector(params.keySet()).elements();
        }
    
        @Override
        public String[] getParameterValues(String name) {
            Object v = params.get(name);
            if (v == null) {
                return null;
            } else if (v instanceof String[]) {
                return (String[]) v;
            } else if (v instanceof String) {
                return new String[] { (String) v };
            } else {
                return new String[] { v.toString() };
            }
        }
    
    }

    2. 在filter 中编写如下代码:

    public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            Map<String, String[]> parametersMap = DataFormat
                    .formatRequestParameters(httpRequest);
            ParameterRequestWrapper requestWrapper = new ParameterRequestWrapper(
                    httpRequest, parametersMap);
            chain.doFilter(requestWrapper, response);
        }
  • 相关阅读:
    微服务定义及.Net Core中用的技术
    IPad分屏,当电脑第二显示屏
    .net Core1.0 邮件发送
    AutoMapper总结
    02-C#(基础)基本的定义和说明
    01-.Net编程机制
    C#基础篇--静态成员、抽象成员、接口
    C#基础篇--面向对象(类与对象)
    期末总结
    改动后的封装
  • 原文地址:https://www.cnblogs.com/tangyanbo/p/4283454.html
Copyright © 2011-2022 走看看