java - Get HTTP status code in asynchronous way -
i http status code before end of dopost method in java. example in python there self.send_response(200)
, sends status in asynchronous way?
/** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse * response) * */ @suppresswarnings("unchecked") @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //final result object //i want send http status code 200 before task running mythreadpool = executors.newfixedthreadpool(2); future taskone = mythreadpool.submit(new runnable() { @override public void run() { try { try { // first task } catch (servletexception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); // task 2 . .
i'm not totally sure intention of code posted, can guess want kind of asynchronous processing, can send data client before request finishes?
for refer asynchronous servlets, described example here: https://blogs.oracle.com/enterprisetechtips/entry/asynchronous_support_in_servlet_3
there example seems serving what, assume, achieve:
@webservlet("/foo" asyncsupported=true) public class myservlet extends httpservlet { public void doget(httpservletrequest req, httpservletresponse res) { ... asynccontext actx = request.startasync(req, res); scheduledthreadpoolexecutor executor = new threadpoolexecutor(10); executor.execute(new asyncwebservice(actx)); } } public class asyncwebservice implements runnable { asynccontext ctx; public asyncwebservice(asynccontext ctx) { this.ctx = ctx; } public void run() { // invoke web service , save result in request attribute // dispatch request render result jsp. ctx.dispatch("/render.jsp"); } }
Comments
Post a Comment