我的过滤器应该编辑响应的 HTML。但是当我导航到一个页面时它什么也不做,因为
newResponse.toString();
返回null
。
进一步调试表明,在该toString
方法中,writer
是null
这是似乎是问题的部分:
public ServletOutputStream getOutputStream() throws IOException {
if (getWriterCalled) {
throw new IllegalStateException("getWriter already called");
}
getOutputStreamCalled = true;
return super.getOutputStream();
}
public PrintWriter getWriter() throws IOException {
if (writer != null) {
return writer;
}
if (getOutputStreamCalled) {
throw new IllegalStateException("getOutputStream already called");
}
getWriterCalled = true;
writer = new PrintWriter(charWriter);
return writer;
}
public String toString() {
String s = null;
if (writer != null) {
s = charWriter.toString();
}
return s;
}
}
完整代码在这里: