0

There are two use cases:

1.response.getWriter().print("");

2.PrintWriter out = response.getWriter();
  out.print("");
  out.close();

Do I always need to close the Writer like showing in the second case? or can I do it as simple as the first one?

Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
  • 1
    Is this in a Servlet context (i.e., the `response` is `HttpServletResponse`)? Because if so, [please read Should one call .close on HttpServletResponse`](https://stackoverflow.com/questions/1159168/should-one-call-close-on-httpservletresponse-getoutputstream-getwriter) – KevinO May 05 '18 at 02:44
  • Yes, your comments helpful – Yu Jiaao May 05 '18 at 02:46

1 Answers1

0

You get writer from response so normally you should not call close() while doing this. The servlet container will close for you.

If you pre-close your writer here, some feature will not work.

For example, your Filter may throw exceptions that IllegalStateException: the output stream has already been closed

Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51