nerdexam
Oracle

1Z0-900 · Question #51

Given the following code: What should you do at line 1 to enable this servlet receive request data without blocking?

The correct answer is D. Define an AsyncListener and assign it to the AsyncContext object. Note on the answer key: There appears to be an error here - B is actually the correct answer for non-blocking request data reading, not D. Why B is correct: Servlet 3.1 introduced non-blocking I/O via ReadListener. You get the ServletInputStream from the request, create a…

Create Java Web Applications using Servlets

Question

Given the following code:

What should you do at line 1 to enable this servlet receive request data without blocking?

Exhibit

1Z0-900 question #51 exhibit

Options

  • AUse a Runnable instance with the start () method of AsyncContext.
  • BDefine a ReadListener and assign it to the request input stream.
  • CCreate a Callable class and delegate this operation to a ManagedExecutorService by using the
  • DDefine an AsyncListener and assign it to the AsyncContext object.

How the community answered

(52 responses)
  • A
    8% (4)
  • B
    13% (7)
  • C
    4% (2)
  • D
    75% (39)

Explanation

Note on the answer key: There appears to be an error here - B is actually the correct answer for non-blocking request data reading, not D.

Why B is correct: Servlet 3.1 introduced non-blocking I/O via ReadListener. You get the ServletInputStream from the request, create a ReadListener implementation (with onDataAvailable(), onAllDataRead(), onError()), and assign it via inputStream.setReadListener(listener). This lets the container call your listener back when data is ready, rather than blocking the thread.

Why the other options are wrong:

  • A - AsyncContext.start(Runnable) offloads work to a new thread but doesn't make I/O non-blocking; the reading can still block that thread.
  • C - A ManagedExecutorService similarly moves blocking work off the request thread but the read operation itself remains blocking.
  • D - An AsyncListener monitors the lifecycle of async processing (completion, timeout, errors); it has no role in reading request body data.

Memory tip: Think "ReadListener for Reading data." The Servlet 3.1 rule is: to make input non-blocking, use ReadListener; to make output non-blocking, use WriteListener. AsyncListener is about lifecycle events, not data I/O.

If your exam insists D is correct, flag it to your instructor - this is a likely answer key error based on the Servlet 3.1 specification.

Topics

#async-servlets#AsyncContext#AsyncListener#non-blocking-io

Community Discussion

No community discussion yet for this question.

Full 1Z0-900 Practice