java—使Servlet2.5代码与Servlet3.0兼容,反之亦然

htrmnn0y  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(309)

我有一个实现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();
        }
    }

    ....

当使用Servlet2.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(Servlet2.5)和tomcat7(Servlet3.0)上运行这个项目,而不必修改intercepthttprequestfilter和intercepthttpresponsefilter?

zdwk9cvp

zdwk9cvp1#

所以建议延长 HttpServletRequestWrapper 而不是实施 HttpServletRequest 直接。 HttpServletRequestWrapper 具有所有必需方法的实现,因此应该能够解决问题。

相关问题