Java Reference
In-Depth Information
Chapter 1
New Servlet Features
Along with the release of Java EE 7 comes an incremental upgrade to Java servlet technology. The servlet technology
has been updated to include features that help bring the technology inline for use with newer technologies, such as
HTML 5. The previous release, Servlet 3.0, focused on making servlets easier to write and manage. Such upgrades
as optional web.xml configuration, asynchronous request processing, and the addition of the FileUpload technology
make writing servlets much easier than before. The Servlet 3.1 release brings forth more enhancements to help bring
the technology inline with current technologies, including the following:
Builds upon the new features of Servlet 3.0, introducing the Non-Blocking I/O API
The ability to work with the WebSockets protocol
Includes security enhancements
Offers other, miscellaneous updates
Non-Blocking I/O
Servlet technology has allowed only traditional (blocking) input/output during request processing since its inception.
In the Servlet 3.1 release, the new Non-Blocking I/O API makes it possible for servlets to read or write without any
blocking. This means that other tasks can be performed at the same time as a read or write is occurring, without any
wait. This in turn means that now you can more easily accomplish Ajax and partial-page refreshes without making
separate calls to the servlet for each update. Such a solution opens up a new realm of possibilities for servlets, making
them much more flexible for use along with modern technologies such as the WebSockets protocol (covered in
Chapter 9).
To implement a nonblocking I/O solution, new programming interfaces have been added to
ServletInputStream and ServletOutputStream , as well as two event listeners: ReadListener and WriteListener .
The ReadListener and WriteListener interfaces make the servlet I/O processing occur in a nonblocking manner
via callback methods that are invoked when the container is able to do so without blocking other processes. Use the
ServletInputStream.setReadListener(ServletInputStream, AsyncContext) method to register a ReadListener
with a ServletInputStream , and use the I/O read ServletInputStream.setWriteListener(ServletOutputStream,
AsyncContext) method for registering a WriteListener . The following lines of code demonstrate how to register a
ReadListener implementation with a ServletInputStream :
AsyncContext context = request.startAsync();
ServletInputStream input = request.getInputStream();
input.setReadListener(new ReadListenerImpl(input, context));
Search WWH ::

Custom Search