2014-01-11 60 views
0

我必須編寫一個過濾器來更改服務器的輸出。這裏是我的代碼:篩選截取服務器輸出

@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) 

所以從一些研究後我的理解ch,是這個錯誤是由於我從ServerClass和TextFilter打開OutputStream引起的。但是我怎樣才能攔截服務器的輸出而不這樣做?在這種情況下哪種方法是正確的?

回答

0

該小服務程序正在調用getOutputStream而不是getWriter。您的class TextResponseWrapper不會覆蓋getOutputStream

所以,你有兩個選擇:

  1. 覆蓋getOutputStream來回報您的包裹流。
  2. 將servlet的代碼從PrintWriter pw = new PrintWriter(response.getOutputStream());更改爲PrintWriter pw = new PrintWriter(response.getWriter());