简体   繁体   English

使Servlet 2.5代码与Servlet 3.0兼容,反之亦然

[英]Making Servlet 2.5 code Servlet 3.0 compatible and vice versa

I have a class that implements javax.servlet.Filter and inside that filter, I'm instantiating an instance of InterceptHttpRequestFilter and InterceptHttpResponseFilter (which are used to modify the incoming and outgoing request and response) 我有一个实现javax.servlet.Filter的类,并且在该过滤器内部,我实例化了InterceptHttpRequestFilter和InterceptHttpResponseFilter的实例(用于修改传入和传出的请求和响应)

Example: 例:

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

    ....

When deploying this project on Tomcat6 using Servlet 2.5, everything works as it should. 当使用Servlet 2.5在Tomcat6上部署该项目时,一切都会正常进行。 Deploy it on Tomcat 7 and I get an AbstractMethodError: 在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)

The obvious solution is to implement all the missing methods required by Servlet Version 3's HttpRequest / Response interfaces. 显而易见的解决方案是实现Servlet版本3的HttpRequest / Response接口所需的所有缺少的方法。

The maven dependency on mvnrepository is still in alpha: Maven对mvnrepository的依赖仍然在alpha中:

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

So my question is, does middle ground exist that will allow me to run this project on both Tomcat6 (Servlet 2.5) and Tomcat7 (Servlet 3.0) without having to tinker with my InterceptHttpRequestFilter and InterceptHttpResponseFilter ? 所以我的问题是,是否存在使我能够在Tomcat6(Servlet 2.5)和Tomcat7(Servlet 3.0)上运行该项目的中间立场,而不必修改我的InterceptHttpRequestFilter和InterceptHttpResponseFilter?

That's why it's recommended to extend HttpServletRequestWrapper instead of implementing HttpServletRequest directly. 这就是为什么建议扩展HttpServletRequestWrapper而不是直接实现HttpServletRequest

HttpServletRequestWrapper has implementations of all required methods, therefore it should solve the problem. HttpServletRequestWrapper具有所有必需方法的实现,因此应该可以解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM