我有一个实现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接口所需的所有缺失方法。
对mvnrepository的maven依赖仍然是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?
答案 0 :(得分:5)
这就是为什么建议扩展HttpServletRequestWrapper
而不是直接实施HttpServletRequest
的原因。
HttpServletRequestWrapper
具有所有必需方法的实现,因此它应该解决问题。