过滤以拦截服务器输出

时间:2014-01-11 15:07:36

标签: servlet-filters

我必须编写一个更改服务器输出的过滤器。这是我的代码:

@WebServlet("/ServerClass")
public class ServerClass extends HttpServlet {

private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String text = request.getParameter("text");

        PrintWriter pw = new PrintWriter(response.getOutputStream());
        pw.write(text);
        pw.close();
    }
}

过滤器实现:

@WebFilter("/TextFilter")
public class TextFilter implements Filter {

    public TextFilter() {
    }

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
         PrintWriter out = response.getWriter();
         TextResponseWrapper wrapper = new TextResponseWrapper((HttpServletResponse)response);

         chain.doFilter(request, wrapper);

         String responseText = wrapper.toString();
         responseText.replaceAll("text", "****");

         response.setContentLength(responseText.getBytes().length);
         out.println(responseText);
         out.close();
     }

    public void init(FilterConfig fConfig) throws ServletException {
    }
}

class TextResponseWrapper extends HttpServletResponseWrapper {

    private CharArrayWriter output;

    public String toString() {
        return output.toString();
    }

    public TextResponseWrapper(HttpServletResponse response){
        super(response);
        output = new CharArrayWriter();
    }

    public PrintWriter getWriter(){
        return new PrintWriter(output);
    }
}

的web.xml

<filter>
    <filter-name>TextFilter</filter-name>
    <filter-class>TextFilter</filter-class>
</filter>
<filter-mapping>
        <filter-name>TextFilter</filter-name>
        <servlet-name>ServerClass</servlet-name>
</filter-mapping>

问题是我收到以下错误:

java.lang.IllegalStateException: getWriter() has already been called for this response
at org.apache.catalina.connector.Response.getOutputStream(Response.java:604)
at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:197)
at javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:96)
at ServerClass.doPost(ServerClass.java:19)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at TextFilter.doFilter(TextFilter.java:33)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

因此,根据我在一些研究后的理解,这个错误是由于我从ServerClass和TextFilter打开OutputStream这一事实引起的。但是如何在不这样做的情况下拦截服务器输出?在这种情况下哪种方法是正确的?

1 个答案:

答案 0 :(得分:0)

servlet正在调用getOutputStream,而不是getWriter。您的class TextResponseWrapper未覆盖getOutputStream

所以你有两个选择:

  1. 覆盖getOutputStream以返回您的包装流。
  2. 将servlet的代码从PrintWriter pw = new PrintWriter(response.getOutputStream());更改为PrintWriter pw = new PrintWriter(response.getWriter());