users@glassfish.java.net

Re: servlet 3.0 async, gfv3

From: Jan Luehe <Jan.Luehe_at_Sun.COM>
Date: Thu, 15 Apr 2010 08:49:20 -0700

Jan Luehe wrote:
> Hi Gary,
>
> emiddio-verizon wrote:
>> thanks for the servlet; i will study and use it.
>>
>> my servlet's init() is being called for each browser request -- i
>> guess i will file a bug; also seen with
>> gfv3 sample async-request-war when debug stepping thru the code.
>
> There is a bug in AjaxCometServlet#init: It is not calling
> super.init(config), thus violating the contract of
> javax.servlet.GenericServlet#init, which reads:
>
> * When overriding this form of the method, call
> * <code>super.init(config)</code>.
>
> So this is a problem specific to AjaxCometServlet, but not a problem
> that would apply to all Servlets that are declared to support async
> operations.
>
> Thanks for catching this!
>
> Please go ahead and file a bug against samples.
>
>>
>> your why not question --
>>
>> servlet1 dispatches with
>> AsyncContext ac = request.startAsync();
>> ...
>> ac.dispatch("servlet2");
>>
>> servlet2 tries to get AsyncContext via
>> request.getAsyncContext(); -- get exception because
>> request.isAsyncStarted() is false;
>>
>> if unable to get the AsyncContext cannot call ac.complete();
>
> We'll look into this.

Actually, this is expected and conformant to the spec.

See javax.servlet.ServletRequest#isAsyncStarted:

     * <p>This method returns <tt>false</tt> if this request was
     * put into asynchronous mode, but has since been dispatched using
     * one of the {_at_link AsyncContext#dispatch} methods or released
     * from asynchronous mode via a call to {_at_link AsyncContext#complete}.

and javax.servlet.AsyncContext#dispatch:

     * <p>Control over the request and response is delegated
     * to the dispatch target, and the response will be closed when the
     * dispatch target has completed execution, unless
     * {_at_link ServletRequest#startAsync()} or
     * {_at_link ServletRequest#startAsync(ServletRequest, ServletResponse)}
     * are called.


Note that the target of an async dispatch need not call
AsyncContext#complete in order to complete the response: The response
will be completed and closed automatically when the async dispatch returns.

If you wanted to call AsyncContext#complete during an async dispatch,
you must call ServletRequest#startAsync again, as was shown in the
Servlet test code I sent a couple of days ago:

                // Second async dispatch
                req.startAsync().complete();


Thanks,

Jan


>
> Thanks,
>
> Jan
>
>>
>> gary
>>
>>
>> Yes, only one init per Servlet, regardless of whether the Servlet
>> is declared to support async operations. Shing Wai has confirmed
>> in an earlier email that this is the case.
>>>
>>>
>>> i have not figured how i am suppose to use
>>> AsycnContext.complete(); if i async.dispach to another servlet
>>> -- that servlet cannot
>>> call AsyncContext.complete();
>>
>> Why not?
>>
>>
>