我想知道它是否符合java servlet规范2.5来在请求之间引用/保存request.getParameterMap()
的返回值。
最终规范仅在第 203 页说明:
返回:包含参数名称作为键的不可变 java.util.Map
和参数值作为映射值。参数映射中的键是
类型字符串。参数映射中的值是字符串数组类型。
但我不清楚:
如果 Map 仅对应用程序不可变,对应用程序服务器不可变
如果允许应用服务器可以将实例重新用于另一个请求(例如,如果参数及其值与上一个请求中的相同)
编辑: 这样做的原因: 我想保存每个请求的请求映射,并在出现错误时将其打印出来以用于诊断目的。
最佳答案
if the Map is only immutable to the application, not to the application server
它在 API 中是不可变的,是的。也有道理,改变请求参数映射有什么意义呢? servletcontainer 实现当然可以“在幕后”保存它的可变映射。但您不应该担心具体的实现细节。
if it's allowed that the application server may re-use the instance for another request (for example, if the parameters and their values are the same as in the previous request)
不,服务器不会这样做。它只是创建一个新的。这些参数绑定(bind)到特定的请求。
如果您想收集 session 期间每个请求的参数,那么您需要自己执行此操作。最好的地方是过滤器
。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
HttpServletRequest hsr = (HttpServletRequest) request;
List<Map<String, String[]>> allParameters = (List<Map<String, String[]>>) hsr.getSession().getAttribute("allParameters");
if (allParameters == null) {
allParameters = new ArrayList<Map<String,String[]>>();
hsr.getSession().setAttribute("allParameters", allParameters);
}
allParameters.add(hsr.getParameterMap());
chain.doFilter(request, response);
}
关于java - 在请求周期之后引用请求参数映射是否可移植?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3353224/