Multiple choice technology web technology

For a given ServletResponse response, which retrieves an object for writing binary data?

  1. response.getWriter()

  2. response.getOutputStream()

  3. response.getOutputWriter()

  4. response.getWriter(Writer.OUTPUT_BINARY)

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

ServletResponse.getOutputStream() returns a ServletOutputStream for writing binary data. The getWriter() method returns a PrintWriter, which is for character/text data only. You cannot call both getOutputStream() and getWriter() on the same response.

AI explanation

To answer this question, you need to understand the difference between writing character data and binary data in a ServletResponse.

Option A) response.getWriter() - This option is incorrect because the getWriter() method retrieves a PrintWriter object that is used for writing character data. It is not used for writing binary data.

Option B) response.getOutputStream() - This option is correct because the getOutputStream() method retrieves a ServletOutputStream object that is used for writing binary data. This is the correct method to use when writing binary data to the ServletResponse.

Option C) response.getOutputWriter() - This option is incorrect because there is no getOutputWriter() method in the ServletResponse interface. This method does not exist.

Option D) response.getWriter(Writer.OUTPUT_BINARY) - This option is incorrect because there is no overloaded getWriter() method that accepts a parameter of type Writer.OUTPUT_BINARY. This method does not exist.

The correct answer is B) response.getOutputStream(). This option is correct because the getOutputStream() method retrieves a ServletOutputStream object that is used for writing binary data to the ServletResponse.