我正在尝试重现this example
我有代码:
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws java.io.IOException, ServletException {
HtmlResponseWrapper capturingResponseWrapper = new HtmlResponseWrapper(
(HttpServletResponse) response);
// Pass request back down the filter chain
chain.doFilter(request,response);
if (response.getContentType() != null
&& response.getContentType().contains("text/html")) {
String content = capturingResponseWrapper.getCaptureAsString();
// replace stuff here
String replacedContent = content.replaceAll(
"<h2[^>]*>(.*?)</h2>",
"<h3>$1 - HTML replaced</h3>");
System.out.println(replacedContent);
response.getWriter().write(replacedContent);
}
通过导航到html页面执行此代码我收到500错误:
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
我滥用了什么吗?如果我摆脱chain.doFilter(request,response);
我没有回复任何例外,但我得到了空白的反应机构。
答案 0 :(得分:1)
您应该将包装器传递给doFilter
方法:
chain.doFilter(request,capturingResponseWrapper);