0

フィルターでサーブレットの応答を変更する際に問題があります。
これが私のコードの一部です:

public class MyFilter implements Filter {

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        System.out.println("BEFORE filter");
        PrintWriter out = response.getWriter();
        CharResponseWrapper responseWrapper = new CharResponseWrapper((HttpServletResponse) response);

        chain.doFilter(request, responseWrapper);

        String servletResponse = new String(responseWrapper.toString());

        out.write(servletResponse);
        out.close();

        System.out.println("Response: " + servletResponse);
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
    }

}

このコードは、サーブレットが html ページを返す場合は正常に機能しますが、サーブレットが xml を返そうとすると、例外が発生します。

SEVERE: Servlet.service() for servlet ViewerServlet threw exception
java.lang.IllegalStateException: getWriter() has already been called for this response
    at org.apache.catalina.connector.Response.getOutputStream(Response.java:579)
    at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183)
    at javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:102)
    at org.apache.axis.transport.http.AxisServlet.sendResponse(AxisServlet.java:902)
    at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:777)
    at org.eclipse.birt.report.servlet.BirtSoapMessageDispatcherServlet.doPost(BirtSoapMessageDispatcherServlet.java:265)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.eclipse.birt.report.servlet.BirtSoapMessageDispatcherServlet.service(BirtSoapMessageDispatcherServlet.java:122)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.epam.bi.filter.RptGenerationTimeCalcFilter.doFilter(RptGenerationTimeCalcFilter.java:34)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.eclipse.birt.report.filter.ViewerFilter.doFilter(ViewerFilter.java:68)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Thread.java:619)

誰もそのような問題に直面していますか?それを機能させる方法はありますか?

前もって感謝します。

4

0 に答える 0