It might be useful to see what line of your code the exception is being thrown by (it'll be later in that stack trace), but I'd guess it's line 9 of that snippet.
I think the problem is the
sendRedirect() call. That tries to set and send the headers of the response, and will throw an exception if the response is already committed. And writing to and/or flushing the output stream will commit the response (i.e. start sending content to the client).
Why are you trying to redirect? I'd expect your
servlet to want to return content or redirect the client, but not both.