views:

371

answers:

3

I don't know, but I feel that IllegalStateException is causing undo headache. If I have a request say a JSP or some other servlet and need to associate a filter with that request. I can't do any other modifications to the output to the client if the response has already been committed?

For example, I see in JSP code that sets the mimetype to response.setContent("html") and then I have a filter associated with the JSP that needs to set the mimetype to something else. setContent("image") or "xhtml". But I get an IllegalStateException?

Is there a way to clear the previous response that has been committed or is there something to avoid this issue.

This is on websphere.

+2  A: 

I think you need to rethink how you're preparing and returning your responses. You can't write to the response (which may write to the client) and then change your mind.

Leaving aside the question of why your solution has to change the return type, I would write to some dummy container object with the return date + type, and make this mutable. Only once your servlet has completed all its work would you then write this object (type+content) to the outputstream.

(to clarify, I don't think servlet filters are the right approach for this. They will intercept the request, and the response, but the response population is the responsibility of the servlet)

Brian Agnew
That is fine, but there is so much going on in the J2EE world that may also modify the response. Basically making it impossible for me to change output mimetypes and other such things.
Berlin Brown
Here's the rub (or a question to consider). At what point does the output get written to the client (the browser). Because once it's got that far, there's no going back :-) Ultimately that's why the response object doesn't let you change attributes etc.
Brian Agnew
Imagine you have a JSP. You have a filter on that jsp. Depending on what the filter does you might do a response.sendRedirect. In websphere 5.1, apparently the JSP by default already commits the resp and sets the mimetype. IllegalState if on sendRedierct
Berlin Brown
I think the problem is that you're starting with the JSP, whereas I suspect the JSP is one possible outcome for a request (another being a different mime-type e.g. PDF)
Brian Agnew
A: 

Your premise to execute a jsp outputting HTML and then have a filter changing the content type to something else like image all in the same response makes no sense. A response can only return a single artefact be it a HTML file or image. There is no way to return both within the same response- any attempt to do so is nonsense . It makes no sense to send some HTML with image binary tacked on the end. The content type is for the entire response. You cannot package multiple thingos in the one response.

Sometimes you can resetBuffer() if the output as not exceeded the buffersize. The best approach is to avoid this facility and deyermi e what to do before you start outputting a response..

mP
That is exactly what I am doing. I am using a html to image parsing tool to take an input html document then output that document to image. It works for some services but on some it doesn't.https://xhtmlrenderer.dev.java.net/
Berlin Brown
+1  A: 

I agree with the other posters that this is ugly but you can create an HttpServletResponseWrapper in your filter that would hijack the output and pass that wrapper to the chain instead of the original response object.

Maurice Perry
I am doing that, and that is where I get the error. At any point in the beginning of the filter chain, if I have set the mimetype, I can't change it by the end of the chain.
Berlin Brown
Well then I guess you would need to hijack the setContentType as well
Maurice Perry