HttpServletRequestWrapper

HttpServletRequestWrapper

我有一个实现javax.servlet.Filter的类,并且在该过滤器内部,我实例化了InterceptHttpRequestFilter和InterceptHttpResponseFilter的实例(用于修改传入和传出的请求和响应)

例:

public class InterceptHttpRequestFilter implements HttpServletRequest {

    private HttpServletRequest httpReq;
    final StringBuffer sb = new StringBuffer();

    public InterceptHttpRequestFilter(ServletRequest request) {
        this.httpReq = (HttpServletRequest) request;
        try {
            StringWriter sw = new StringWriter();
            IOUtils.copy(request.getInputStream(), sw);
            sb.append(sw.getBuffer().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    ....


当使用Servlet 2.5在Tomcat6上部署该项目时,一切都会正常进行。
在Tomcat 7上部署它,我得到了AbstractMethodError:

SEVERE: Servlet.service() for servlet [_______] in context with path [/__________-1.0.0] threw exception [Filter execution threw an exception] with root cause
java.lang.AbstractMethodError
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:225)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at com.mycee.project.filter.MyFilter.doFilter(MyFilter.java:182)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:744)


显而易见的解决方案是实现Servlet版本3的HttpRequest / Response接口所需的所有缺少的方法。

Maven对mvnrepository的依赖仍然在alpha中:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>3.0-alpha-1</version>
</dependency>


所以我的问题是,是否存在使我能够在Tomcat6(Servlet 2.5)和Tomcat7(Servlet 3.0)上运行该项目的中间立场,而不必修改我的InterceptHttpRequestFilter和InterceptHttpResponseFilter?

最佳答案

这就是为什么建议扩展HttpServletRequestWrapper而不是直接实现HttpServletRequest的原因。

HttpServletRequestWrapper具有所有必需方法的实现,因此应该可以解决问题。

关于java - 使Servlet 2.5代码与Servlet 3.0兼容,反之亦然,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22323986/

10-11 09:21