筛选截取服务器输出

我必须编写一个过滤器来更改服务器的输出。这里是我的代码:筛选截取服务器输出

@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引起的。但是我怎样才能拦截服务器的输出而不这样做?在这种情况下哪种方法是正确的?

回答:

该小服务程序正在调用getOutputStream而不是getWriter。您的class TextResponseWrapper不会覆盖getOutputStream

所以,你有两个选择:

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

以上是 筛选截取服务器输出 的全部内容, 来源链接: utcz.com/qa/261057.html

回到顶部