当使用HttpServletRequestWrapper包装原始请求时,jsp:param不再设置参数

时间:2010-02-03 18:56:40

标签: java jsp jsp-tags

我有一个过滤器接收传入请求,然后使用HttpServletRequestWrapper包装它,而HttpServletRequestWrapper又有一个setParameter()方法。但是,现在在任何已过滤的servlet中都不再有效:

<jsp:include page="testing-include.jsp">
    <jsp:param name="testing" value="testing" />
</jsp:include>

包含页面不会接受请求参数。如果我删除过滤器,并将原始未修改的请求发送(解包)到servlet,则它再次起作用。这是我的包装器:

public class HttpServletModifiedRequestWrapper extends HttpServletRequestWrapper {

    Map parameters;

    @SuppressWarnings("unchecked")
    public HttpServletModifiedRequestWrapper(HttpServletRequest httpServletRequest) {
        super(httpServletRequest);
        parameters = new HashMap(httpServletRequest.getParameterMap());
    }

    public String getParameter(String name) {
        String returnValue = null;
        String[] paramArray = getParameterValues(name);
        if (paramArray != null && paramArray.length > 0){
            returnValue = paramArray[0];
        }
        return returnValue;
    }

    @SuppressWarnings("unchecked")
    public Map getParameterMap() {
        return Collections.unmodifiableMap(parameters);
    }

    @SuppressWarnings("unchecked")
    public Enumeration getParameterNames() {
        return Collections.enumeration(parameters.keySet());
    }

    public String[] getParameterValues(String name) {
        String[] result = null;
        String[] temp = (String[]) parameters.get(name);
        if (temp != null){
            result = new String[temp.length];
            System.arraycopy(temp, 0, result, 0, temp.length);
        }
        return result;
    }

    public void setParameter(String name, String value){
        String[] oneParam = {value};
        setParameter(name, oneParam);
    }

    @SuppressWarnings("unchecked")
    public void setParameter(String name, String[] values){
        parameters.put(name, values);
    }
}

在没有查看Tomcat的jsp:include和jsp:param标准操作的实现源的情况下,我确实无法确定可能发生的事情,但是必须存在一些冲突。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我想问题是你的包装器不能提供对新参数的访问,这些参数在复制后会被添加到原始参数图中。

可能你应该做这样的事情(以及其他方法):

public String[] getParameterValues(String name) {  
    String[] result = null;  
    String[] temp = (String[]) parameters.get(name);  
    if (temp != null){  
        result = new String[temp.length];  
        System.arraycopy(temp, 0, result, 0, temp.length);  
    } else {
        return super.getParameterValues(name);
    } 
    return result;  
}