本文整理汇总了Java中javax.servlet.ServletOutputStream.setWriteListener方法的典型用法代码示例。如果您正苦于以下问题:Java ServletOutputStream.setWriteListener方法的具体用法?Java ServletOutputStream.setWriteListener怎么用?Java ServletOutputStream.setWriteListener使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.ServletOutputStream
的用法示例。
在下文中一共展示了ServletOutputStream.setWriteListener方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getOutputStream
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
public ServletOutputStream getOutputStream() throws IOException {
final ServletOutputStream outputStream = d.getOutputStream();
return new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
respBody.write(b);
outputStream.write(b);
}
@Override
public void setWriteListener(WriteListener writeListener) {
outputStream.setWriteListener(writeListener);
}
@Override
public boolean isReady() {
return outputStream.isReady();
}
};
}
示例2: processRequest
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
AsyncContext context = request.startAsync();
ServletOutputStream output = response.getOutputStream();
output.setWriteListener(new WritingListener(output, context));
}
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:9,代码来源:WritingServlet.java
示例3: handle
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
public void handle(String path, Request request, final HttpServletRequest servletRequest,
final HttpServletResponse servletResponse) throws IOException, ServletException {
request.setHandled(true);
switch (path) {
case "/hello":
servletResponse.setStatus(200);
servletResponse.setContentType("text/plain");
servletResponse.getWriter().write("Hello World!");
break;
case "/hello-async":
final AsyncContext context = request.startAsync();
Thread t = new Thread(() -> {
servletResponse.setStatus(200);
servletResponse.setContentType("text/plain");
final ServletOutputStream outputStream;
try {
outputStream = servletResponse.getOutputStream();
outputStream.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
PrintWriter w = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"));
w.write("Hello World!");
w.close();
context.complete();
}
@Override
public void onError(Throwable throwable) {
context.complete();
}
});
} catch (IOException e) {
context.complete();
}
});
t.start();
break;
default:
servletResponse.setStatus(404);
servletResponse.setContentType("text/plain");
servletResponse.getWriter().write("Not found.");
break;
}
}