<div dir="ltr"><div>Hi Stuart,</div><div><br></div><div>It worked!  I was able to get the servletcontext and then pass it into the handler as you suggested.  Like this:</div><div><br></div><div><div>================================<br>
</div></div><div><div><div>public class NonBlockingHandlerExtension implements ServletExtension {</div><div>    @Override</div><div>    public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) {</div>
<div>        final ServletContextImpl srvctx = (ServletContextImpl) servletContext;</div><div>        System.out.println(&quot;\n\n\n ... \n\n\n srvctx=&quot;+srvctx);</div><div>        //returns non-null object</div><div>
<br></div><div>        deploymentInfo.addInitialHandlerChainWrapper(new HandlerWrapper() {</div><div>            @Override</div><div>            public HttpHandler wrap(final HttpHandler handler) {</div><div>            <span class="" style="white-space:pre">        </span>HelloWorldHandler hwhandler = new HelloWorldHandler();</div>
<div>            <span class="" style="white-space:pre">        </span>hwhandler.setServletContext(srvctx);</div><div>                return new PathHandler()</div><div>                <span class="" style="white-space:pre">                </span>.addExactPath(&quot;/hello&quot;, hwhandler)</div>
<div>                <span class="" style="white-space:pre">                </span>.addPrefixPath(&quot;/&quot;, handler);</div><div>            }</div><div>        });</div><div>    }</div><div>}<br></div></div><div><br></div><div><br>
</div><div>then the handler:<br></div></div><div><br></div><div><div><div>public class HelloWorldHandler implements HttpHandler {</div><div><span class="" style="white-space:pre">        </span>public ServletContextImpl srvctx;</div>
<div><span class="" style="white-space:pre">        </span>public void setServletContext(ServletContextImpl _srvctx) {</div><div><span class="" style="white-space:pre">                </span>srvctx = _srvctx;</div><div><span class="" style="white-space:pre">        </span>}</div>
<div><span class="" style="white-space:pre">        </span></div><div>    @Override</div><div>    public void handleRequest(final HttpServerExchange exchange) throws Exception {</div></div></div><div><div>        HttpSessionImpl sessionimpl;</div>
<div>        sessionimpl = srvctx.getSession(exchange, true);<br></div><div>        System.out.println(&quot;sessionimpl(exchange)=&quot;+sessionimpl);</div><div>        System.out.println(&quot;1. PRE session get coffee=&quot;+sessionimpl.getAttribute(&quot;coffee&quot;));</div>
<div>        sessionimpl.setAttribute(&quot;coffee&quot;, &quot;Stumptown&quot;);</div><div>        System.out.println(&quot;1. POST session get coffee=&quot;+sessionimpl.getAttribute(&quot;coffee&quot;));</div></div><div>
<br></div><div><br></div><div><div>================================<br></div></div><div><br></div><div>My only concern now is with thread safety and synchronization.  </div><div><br></div><div>Will instantiating a new handler and then passing the servlet context via a public set method be thread safe?  Will it introduce any kind of synchronization contention?  </div>
<div><br></div><div>And is there a &quot;better&quot; way of passing that servlet context into the handler?  </div><div><br></div><div>Also, if it has been there this whole time, how come </div><div><div><br></div><div><div>
       ServletRequestContext reqctx = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);</div></div><div><br></div><div>inside the handleRequest method does not work?</div><div><br></div><div>Thanks!</div><div>
<br></div><div><br></div><div><br></div></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Mar 10, 2014 at 1:37 PM, Stuart Douglas <span dir="ltr">&lt;<a href="mailto:sdouglas@redhat.com" target="_blank">sdouglas@redhat.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Yes, if you are running handlers outside the servlet chain the servlet request context does not get setup.<br>
<br>
Because you are actually after the ServletContextImpl (which allows you to get the session) you need to get a reference to this at deployment time.<br>
<br>
How are you setting up your non-blocking handler? Via ServletExtension?<br>
<br>
If so then the ServletContext is just passed into the extension, so you can just pass it directly to the handler (You will need to cast it to ServletContextImpl though in order to use it to get the session).<br>
<br>
Stuart<br>
<br>
Lightspoke Discussion wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">
Hi Stuart,<br>
<br>
I have a regular servlet (the sample... public class HelloWorldServlet<br>
extends HttpServlet) running inside the same web context (in the same<br>
war).  In the HelloWorldServlet I can access the session object.  I ran<br>
that first, but when I run the non-blocking servlet, my reqctx still<br>
returns null.<br>
<br>
I&#39;m running Wildfly 8.0.0 final, in case that matters.<br>
<br>
So is everything hinging on:<br>
<br>
<br>
public class ServletInitialHandler implements HttpHandler,<br>
ServletDispatcher {<br>
<br>
...<br>
publicvoidhandleRequest(<u></u>finalHttpServerExchangeexchang<u></u>e)throwsIOException,<u></u>ServletException<br>
...<br>
<br>
exchange.putAttachment(<u></u>ServletRequestContext.<u></u>ATTACHMENT_KEY,<br>
servletRequestContext);<br>
<br>
...<br>
<br>
<br>
<br>
<br>
<br>
On Sun, Mar 9, 2014 at 12:55 PM, Stuart Douglas &lt;<a href="mailto:sdouglas@redhat.com" target="_blank">sdouglas@redhat.com</a><br></div><div class="">
&lt;mailto:<a href="mailto:sdouglas@redhat.com" target="_blank">sdouglas@redhat.com</a>&gt;&gt; wrote:<br>
<br>
    This will only be not null after the first servlet handler has run.<br>
    If your handler is executing outside the scope of the servlet<br>
    deployment then you need to store a reference to the ServletContext<br>
    somewhere (worst case = stick it in a static somewhere).<br>
    Stuart<br>
<br>
<br>
    Lightspoke Discussion wrote:<br>
<br>
        If you are running before the initial handler you need to store<br>
        a reference to the servlet context or the Deployment somewhere.<br>
<br>
<br>
<br>
<br>
--<br>
--<br>
Matthew Ma<br>
<a href="http://www.lightspoke.com" target="_blank">http://www.lightspoke.com</a><br>
Lightspoke Web Based Database<br>
</div></blockquote>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>--<br>Matthew Ma<br><a href="http://www.lightspoke.com">http://www.lightspoke.com</a><br>Lightspoke Web Based Database
</div>