I need to remove/modify some of HttpServletRequest parameters. I’m trying to do so by using a Filter based on the question I posted a few days ago.
In the Filter, I’m trying to wrap the HttpServletRequest by inheriting the HttpServletRequestWrapper class as follows.
private final static class FilteredRequest extends HttpServletRequestWrapper {
public FilteredRequest(ServletRequest request) {
super((HttpServletRequest) request);
}
@Override
public String getParameter(String paramName) {
return super.getParameter(paramName);
}
@Override
public String[] getParameterValues(String paramName) {
return super.getParameterValues(paramName);
}
@Override
public Map getParameterMap() {
Map<Object, Object> parameterMap = new HashMap<Object, Object>();
Map originalParameterMap = super.getParameterMap();
for (Object o : originalParameterMap.entrySet()) {
Map.Entry<Object, Object> pairs = (Entry<Object, Object>) o;
parameterMap.put(pairs.getKey(), pairs.getValue());
}
return parameterMap; //Returning a modifiable ParameterMap.
}
}
It’s an inner class within the Filter class. In the doFilter() method,
chain.doFilter(new FilteredRequest(request), (HttpServletResponse)response);
the constructor of the above class is being invoked (that wraps the request).
Now, I expect any of request parameters to modify/unset in my Spring MVC controller class. I’m trying to remove the parameter in the controller class as follows.
Map requestMap=request.getParameterMap();
requestMap.remove("txt_country_name");
Alternatively,
requestMap.put("txt_country_name", null);
Accordingly, the request parameter txt_country_name should be removed from the HttpServletRequest but it isn’t removed (nor it throws any exception like “No modifications are allowed to a locked ParameterMap“). What am I missing here? Am I following a wrong way?
By the way, Making/using a request attribute all the time through out the entire application doesn’t seem to be the best solution.
[I need to remove/modify request parameter within the Spring MVC controller class and not within the Filter itself]
Your remove method modifies the mutable map returned by
getParameterMap(). But the next call togetParameterMap()reconstructs a new mutable map containing all the parameters of the wrapped query.The mutable map should be constructed when the
FilteredRequestis constructed, and it should be stored in an instance field.