How to set a parameter in a HttpServletRequest?

后端 未结 7 695
感动是毒
感动是毒 2021-02-01 03:16

I am using a javax.servlet.http.HttpServletRequest to implement a web application.

I have no problem to get the parameter of a request using the getParamete

相关标签:
7条回答
  • 2021-02-01 03:27

    The missing getParameterMap override ended up being a real problem for me. So this is what I ended up with:

    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;
    
    /***
     * Request wrapper enabling the update of a request-parameter.
     * 
     * @author E.K. de Lang
     *
     */
    final class HttpServletRequestReplaceParameterWrapper
        extends HttpServletRequestWrapper
    {
    
        private final Map<String, String[]> keyValues;
    
        @SuppressWarnings("unchecked")
        HttpServletRequestReplaceParameterWrapper(HttpServletRequest request, String key, String value)
        {
            super(request);
    
            keyValues = new HashMap<String, String[]>();
            keyValues.putAll(request.getParameterMap());
            // Can override the values in the request
            keyValues.put(key, new String[] { value });
    
        }
    
        @SuppressWarnings("unchecked")
        HttpServletRequestReplaceParameterWrapper(HttpServletRequest request, Map<String, String> additionalRequestParameters)
        {
            super(request);
            keyValues = new HashMap<String, String[]>();
            keyValues.putAll(request.getParameterMap());
            for (Map.Entry<String, String> entry : additionalRequestParameters.entrySet()) {
                keyValues.put(entry.getKey(), new String[] { entry.getValue() });
            }
    
        }
    
        @Override
        public String getParameter(String name)
        {
            if (keyValues.containsKey(name)) {
                String[] strings = keyValues.get(name);
                if (strings == null || strings.length == 0) {
                    return null;
                }
                else {
                    return strings[0];
                }
            }
            else {
                // Just in case the request has some tricks of it's own.
                return super.getParameter(name);
            }
        }
    
        @Override
        public String[] getParameterValues(String name)
        {
            String[] value = this.keyValues.get(name);
            if (value == null) {
                // Just in case the request has some tricks of it's own.
                return super.getParameterValues(name);
            }
            else {
                return value;
            }
        }
    
        @Override
        public Map<String, String[]> getParameterMap()
        {
            return this.keyValues;
        }
    
    }
    
    0 讨论(0)
  • 2021-02-01 03:31

    Sorry, but why not use the following construction:

    request.getParameterMap().put(parameterName, new String[] {parameterValue});
    
    0 讨论(0)
  • 2021-02-01 03:34

    The most upvoted solution generally works but for Spring and/or Spring Boot, the values will not wire to parameters in controller methods annotated with @RequestParam unless you specifically implemented getParameterValues(). I combined the solution(s) here and from this blog:

    import java.util.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;
    
    public class MutableHttpRequest extends HttpServletRequestWrapper {
    
        private final Map<String, String[]> mutableParams = new HashMap<>();
    
        public MutableHttpRequest(final HttpServletRequest request) {
            super(request);
        }
    
        public MutableHttpRequest addParameter(String name, String value) {
            if (value != null)
                mutableParams.put(name, new String[] { value });
    
            return this;
        }
    
        @Override
        public String getParameter(final String name) {
            String[] values = getParameterMap().get(name);
    
            return Arrays.stream(values)
                    .findFirst()
                    .orElse(super.getParameter(name));
        }
    
        @Override
        public Map<String, String[]> getParameterMap() {
            Map<String, String[]> allParameters = new HashMap<>();
            allParameters.putAll(super.getParameterMap());
            allParameters.putAll(mutableParams);
    
            return Collections.unmodifiableMap(allParameters);
        }
    
        @Override
        public Enumeration<String> getParameterNames() {
            return Collections.enumeration(getParameterMap().keySet());
        }
    
        @Override
        public String[] getParameterValues(final String name) {
            return getParameterMap().get(name);
        }
    }
    
    

    note that this code is not super-optimized but it works.

    0 讨论(0)
  • 2021-02-01 03:40

    From your question, I think what you are trying to do is to store something (an object, a string...) to foward it then to another servlet, using RequestDispatcher(). To do this you don't need to set a paramater but an attribute using

    void setAttribute(String name, Object o);
    

    and then

    Object getAttribute(String name);
    
    0 讨论(0)
  • 2021-02-01 03:41

    If you really want to do this, create an HttpServletRequestWrapper.

    public class AddableHttpRequest extends HttpServletRequestWrapper {
    
       private HashMap params = new HashMap();
    
       public AddableingHttpRequest(HttpServletRequest request) {
               super(request);
       }
    
       public String getParameter(String name) {
               // if we added one, return that one
               if ( params.get( name ) != null ) {
                     return params.get( name );
               }
               // otherwise return what's in the original request
               HttpServletRequest req = (HttpServletRequest) super.getRequest();
               return validate( name, req.getParameter( name ) );
       }
    
       public void addParameter( String name, String value ) {
               params.put( name, value );
       }
    
    }
    
    0 讨论(0)
  • 2021-02-01 03:43

    You can't, not using the standard API. HttpServletRequest represent a request received by the server, and so adding new parameters is not a valid option (as far as the API is concerned).

    You could in principle implement a subclass of HttpServletRequestWrapper which wraps the original request, and intercepts the getParameter() methods, and pass the wrapped request on when you forward.

    If you go this route, you should use a Filter to replace your HttpServletRequest with a HttpServletRequestWrapper:

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if (servletRequest instanceof HttpServletRequest) {
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            // Check wether the current request needs to be able to support the body to be read multiple times
            if (MULTI_READ_HTTP_METHODS.contains(request.getMethod())) {
                // Override current HttpServletRequest with custom implementation
                filterChain.doFilter(new HttpServletRequestWrapper(request), servletResponse);
                return;
            }
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }
    
    0 讨论(0)
提交回复
热议问题