是否可以添加到请求的可用参数(HttpServletRequest)

时间:2021-12-28 16:55:41

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'方法,并且操作时参数map会抛出一个错误,说明它已被锁定。我可以尝试一种替代方案吗?

6 个解决方案

#1


12  

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.

在过滤器中,将请求包装在子类的实例中。

#2


6  

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.

我ussualy将原始的HttpServletRequest包装到一个新的CustomHttpServletRequest中,该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);
}

#3


1  

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是不可变的,没有办法改变它。

#4


1  

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

#5


0  

Why don't you just store variables as Request scope attributes instead of trying to append them to the request parameters?

为什么不将变量存储为请求范围属性而不是尝试将它们附加到请求参数?

#6


-1  

Otherwise, you can use the setAttribute() method which is strongly typed. Therefore, the getAttribute() method can be used ...

否则,您可以使用强类型的setAttribute()方法。因此,可以使用getAttribute()方法......

#1


12  

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.

在过滤器中,将请求包装在子类的实例中。

#2


6  

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.

我ussualy将原始的HttpServletRequest包装到一个新的CustomHttpServletRequest中,该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);
}

#3


1  

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是不可变的,没有办法改变它。

#4


1  

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

#5


0  

Why don't you just store variables as Request scope attributes instead of trying to append them to the request parameters?

为什么不将变量存储为请求范围属性而不是尝试将它们附加到请求参数?

#6


-1  

Otherwise, you can use the setAttribute() method which is strongly typed. Therefore, the getAttribute() method can be used ...

否则,您可以使用强类型的setAttribute()方法。因此,可以使用getAttribute()方法......