当原始请求用HttpServletRequestWrapper包装时,jsp:param不再设置参数 [英] jsp:param no longer sets parameters when original request is wrapped with a HttpServletRequestWrapper

查看:64
本文介绍了当原始请求用HttpServletRequestWrapper包装时,jsp:param不再设置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个过滤器,该过滤器接收传入的请求,然后使用HttpServletRequestWrapper对其进行包装,而HttpServletRequestWrapper上又具有setParameter()方法.但是,这现在在任何过滤的servlet中将不再起作用:

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

包含页面将不会使用request参数.如果删除过滤器,并且原始未修改的请求被发送(解包)到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标准操作的实现源,我真的很难确定会发生什么,但是那里肯定有一些冲突.任何帮助将不胜感激.

解决方案

我想问题是您的包装程序不提供对新参数的访问,这些新参数是在复制后添加到原始参数图中的. >

可能,您应该执行以下操作(以及其他方法):

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

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.

解决方案

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

这篇关于当原始请求用HttpServletRequestWrapper包装时,jsp:param不再设置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆