我有一个案例,我需要将我的HTTP请求对象重定向到其他控制器/类进行进一步处理.问题是在某些控制器中,我希望能够更好地控制我转发到下一个类的参数:修改,编辑,删除它们.所以,我想
class MyHttpServletRequestWrapper implements HttpServletRequest { private HttpServletRequest originalRequest; public MyHttpServletRequestWrapper(HttpServletRequest originalRequest) { this.originalRequest = originalRequest; public String getAuthType() {return originalRequest.getAuthType();} public String getQueryString() {return originalRequest.getQueryString();} // etc. public Map getParameterMap() { Map params = originalRequest.getParameterMap(); params.remove("parameter-to-remove"); params.put("parameter-to-add", "<a value>"); //etc. } }
你的servlet过滤器:
class MyFilter implements Filter { public void init(FilterConfig config) { // perhaps you might want to initialize something here } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { HttpServletRequest originalRequest = (HttpServletRequest) request; HttpServletRequest newRequest = new MyHttpServletRequest(originalRequest); chain.doFilter(newRequest, response); } }
您还可以将javax.servlet.request.HttpServletRequestWrapper子类化,这将为您节省大量工作.
有关更多信息,请参阅此post.