Java Reference
In-Depth Information
request, HttpServletResponse response) method represented in the fol-
lowing code. As you can see, the data reception has been delegated to a listener
( ReadListenerImpl ), which will be notified whenever a new package is available.
This prevents the server from being blocked while waiting for new packages.
protected void doGet(HttpServletRequest
request, HttpServletResponse response) {
try (ServletInputStream input =
request.getInputStream();
FileOutputStream outputStream = new
FileOutputStream("MyFile");) {
AsyncContext context =
request.startAsync();
input.setReadListener(new
ReadListenerImpl(context, input,outputStream));
}catch (IOException ex) {
logger.log(Level.SEVERE, null, ex);
}
}
The implementation of ReadListenerImpl used in the preceding code snippet is
as follows:
public class ReadListenerImpl implements
ReadListener {
AsyncContext context;
ServletInputStream input;
FileOutputStream outputStream;
public ReadListenerImpl(AsyncContext c,
ServletInputStream i, FileOutputStream f) {
this.context = c;
this.input = i;
outputStream = f;
}
Search WWH ::




Custom Search