Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 130517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:57:38+00:00 2026-05-11T05:57:38+00:00

This may be a little confusing but I having some trouble. My goal is

  • 0

This may be a little confusing but I having some trouble. My goal is to take an input HTML document and then process that document and use the HTML data to output a image document. For example, a user will request a URL, with some action=png in the querystring and then the filter will get invoked for the URL and output image document.

I have tried everything but in my environment (Websphere), I am only able to output of one type. If the input type is text/html then I can only output a text document, I can’t seem to output a binary document. Why? Because I get an illegal state exception each time.

[1/29/09 17:59:57:576 EST] 00000020 SystemErr R java.lang.IllegalStateException: SRVE0209E: Writer already obtained [1/29/09 17:59:57:576 EST] 00000020 SystemErr R at com.ibm.ws.webcontainer.srt.SRTServletResponse.getOutputStream(SRTServletResponse.java:505)

I am using pseudo code not to reveal all of my code:

<filter>     <filter-name>TestFilter</filter-name>     <filter-class>         com.util.TestFilter     </filter-class> </filter>  <filter-mapping>     <filter-name>TestFilter</filter-name>     <url-pattern>/index_test2.html</url-pattern> </filter-mapping> 

The Wrapper Class code is essentially this:

public class ContentCaptureServletResponse extends HttpServletResponseWrapper {      private ByteArrayOutputStream contentBuffer;     private PrintWriter writer;      public PrintWriter getWriter() throws IOException {         if (writer == null) {             contentBuffer = new ByteArrayOutputStream();             writer = new PrintWriter(contentBuffer);         }                return writer;     }        public String getContent(){         try {             writer = getWriter();         } catch (IOException e) {                        e.printStackTrace();         }         writer.flush();         String xhtmlContent = new String(contentBuffer.toByteArray());                          System.out.println(xhtmlContent);                         return xhtmlContent;      } } 

And the filter code is this:

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {          HttpServletRequest request = (HttpServletRequest) req;         HttpServletResponse response = (HttpServletResponse) resp;         final String renderType = request.getParameter('R');          final String renderClassName = request.getParameter('C');         if ((renderType != null) && (renderClassName != null)) {             try {                 this.setFilterChain(filterChain);                 response.setContentType('image/png');                 PrintWriter out = response.getWriter();                                              // I call getWriter but not BOTH!                 //response.getOutputStream();                                response.getWriter();                  // Build up the HTML document into a string.                     CaptureResponse capContent = new CaptureResponse(response);                             this.mainFilterChain.doFilter(req, );                 String myString = capHtmlContent.getContent();                  // What I really want to do here is output an output stream                 // so I can write a binary image                 processStr(myString);                 response.getOutputStream();                              response.write(binaryimage)              } catch (Exception e) {                             e.printStackTrace();             }             // No data will be output to the user.         } else {             filterChain.doFilter(request, response);         }  // End of if-else             } // End of the method. 

The code works if I want to take some input html text document. I am assuming because of the open printwriter stream. But I am having trouble going to a different format. Basically, because I can’t call response.getOutputStream()

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:57:38+00:00Added an answer on May 11, 2026 at 5:57 am

    The issue does appear to be that you are opening the response Writer prior to wrapping the response.

    It looks like you should be able to do:

    this.setFilterChain(filterChain); CaptureContent capContent = new CaptureResponse(response); doFilter() process(); response.setContentType('image/png'); response.getOutputStream().write(imagedata); 

    You definitely cannot safely open both the Writer and the OutputStream

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So this may be a little tricky, but take a look at the div
I realize something like this has been asked, but this may be a little
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
This need may sound a little convoluted, and if so, then I am open
This problem may be a little broad, but I'm going to go ahead and
This may seem a little upside down faced, but what I want to be
this may seem a little odd, but it would make for a convenient way
I know this may sound silly little bit, but there is no other way
This may be a little subjective, but I have often found that it can
This may be little weird, but I want to persist only the scalar properties

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.