Java 是否可以添加到请求的可用参数(HttpServletRequest)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/190833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to add to the available parameters of a request (HttpServletRequest)
提问by Vivek Kodira
I want to intercept a request in a filter/servlet and add a few parameters to it. However, the request does not expose a 'setParameter' method and the parameter map when manipulated throws an error saying it is locked. Is there an alternative I can try?
我想拦截过滤器/servlet 中的请求并为其添加一些参数。但是,该请求不会公开“setParameter”方法,并且在操作时参数映射会引发错误,指出它已被锁定。有没有我可以尝试的替代方法?
采纳答案by Bruno De Fraine
Subclass HttpServletRequestWrapper
and override the getParameter
methods. The description of this class reads:
子类化HttpServletRequestWrapper
并覆盖getParameter
方法。这个类的描述如下:
Provides a convenient implementation of the HttpServletRequest interface that can be subclassed by developers wishing to adapt the request to a Servlet.
提供 HttpServletRequest 接口的便捷实现,希望将请求调整为 Servlet 的开发人员可以将其子类化。
In the filter, wrap the request in an instance of your subclass.
在过滤器中,将请求包装在子类的实例中。
回答by m_pGladiator
First you should receive the request and read all its parameters. Then construct another request with the original parameters + the new ones and send it again.
首先,您应该收到请求并读取其所有参数。然后使用原始参数+新参数构造另一个请求并再次发送。
The HttpServletRequest is immutable and there is no way to change it.
HttpServletRequest 是不可变的,没有办法改变它。
回答by Panagiotis Korros
I ussualy wrap the original HttpServletRequest into a new CustomHttpServletRequest that acts as a proxy to the original request and then pass this new CustomHttpServletRequest to the filter chain.
我通常将原始 HttpServletRequest 包装到一个新的 CustomHttpServletRequest 中,作为原始请求的代理,然后将这个新的 CustomHttpServletRequest 传递给过滤器链。
In this CustomHttpServletRequest you can overide the getParameterNames, getParameter, getParameterMap methods to return any parameters you want.
在这个 CustomHttpServletRequest 中,您可以覆盖 getParameterNames、getParameter、getParameterMap 方法以返回您想要的任何参数。
This is an example of the filter:
这是过滤器的示例:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletRequest customRequest = new CustomHttpServletRequest(httpRequest);
customRequest.addParameter(xxx, "xxx");
chain.doFilter(customRequest, response);
}
回答by Bruno De Fraine
Otherwise, you can use the setAttribute() method which is strongly typed. Therefore, the getAttribute() method can be used ...
否则,您可以使用强类型的 setAttribute() 方法。因此,可以使用 getAttribute() 方法...
回答by bpapa
Why don't you just store variables as Request scope attributes instead of trying to append them to the request parameters?
为什么不将变量存储为请求范围属性而不是尝试将它们附加到请求参数?
回答by kolobok
You can wrap HttpServletRequest into new HttpServletRequestWrapper object and overwrite some methods.
您可以将 HttpServletRequest 包装到新的 HttpServletRequestWrapper 对象中并覆盖一些方法。
The following code is from http://www.ocpsoft.org/opensource/how-to-safely-add-modify-servlet-request-parameter-values/.
以下代码来自http://www.ocpsoft.org/opensource/how-to-safely-add-modify-servlet-request-parameter-values/。
To add parameter in filter:
在过滤器中添加参数:
public class MyFilter implements Filter {
...
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest) {
HttpServletRequest httprequest = (HttpServletRequest) request;
Map<String, String[]> extraParams = new HashMap<String, String[]>();
extraParams.put("myparamname", String[] { "myparamvalue" });
request = new WrappedRequestWithParameter(httprequest, extraParams);
}
chain.doFilter(request, response);
}
...
class WrappedRequestWithParameter extends HttpServletRequestWrapper {
private final Map<String, String[]> modifiableParameters;
private Map<String, String[]> allParameters = null;
public WrappedRequestWithParameter(final HttpServletRequest request, final Map<String, String[]> additionalParams) {
super(request);
modifiableParameters = new TreeMap<String, String[]>();
modifiableParameters.putAll(additionalParams);
}
@Override
public String getParameter(final String name) {
String[] strings = getParameterMap().get(name);
if (strings != null) {
return strings[0];
}
return super.getParameter(name);
}
@Override
public Map<String, String[]> getParameterMap() {
if (allParameters == null) {
allParameters = new TreeMap<String, String[]>();
allParameters.putAll(super.getParameterMap());
allParameters.putAll(modifiableParameters);
}
// Return an unmodifiable collection because we need to uphold the interface contract.
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);
}
}
}