Search code examples
javajspjsp-tags

jsp:param no longer sets parameters when original request is wrapped with a HttpServletRequestWrapper


I have a filter that takes an incoming request, and then wraps it with an HttpServletRequestWrapper, which in turn has a setParameter() method on it. However, this will no longer work now in any filtered servlets:

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

The include page will not take the request parameter. If I remove the filter, and the original unmodified request is sent (unwrapped) to the servlet, then it works again. Here is my wrapper:

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);
    }
}

I'm really having trouble determining what could be happening without looking at Tomcat's implementation source for the jsp:include and jsp:param standard actions, but there must be some conflict there. Any help would be appreciated.


Solution

  • I guess the problem is that your wrapper doesn't provide access to the new parameters, which were added to the original parameter map after you copied it.

    Probably, you should do something like this (and in the other methods, too):

    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;  
    }