title: How do I get the org.eclipse.jetty.server.Request from within my servlet
body: <p>Consider this bit of code.  It's a work in progress but the goal is
  to get my hands on an NIO channel that I can use to create a full-duplex
  RubyIO instance (org.jruby.RubyIO), that is selectable by an NIO Selector.</p>
  
  <p>But first I want an NIO Channel ...</p>
  
  <pre><code>        //  This call returns a `javax.servlet.ServletRequest` but the underlying
          // instance was created by Jetty - it seems to me - and is actually an
          // `org.eclipse.jetty.server.Request`
          ServletRequest srv_req = this.http_servlet_request_wrapper.getRequest();
          Request req;
          ClassLoader cl1 = Request.class.getClassLoader();
          ClassLoader cl2 = srv_req.getClass().getClassLoader();
  
          System.out.println("DBG: cl1: " + cl1.toString());
          // =&gt; DBG: cl1: WebAppClassLoader{66233253}@3f2a3a5
  
          System.out.println("DBG: cl2: " + cl2.toString());
          // =&gt; DBG: cl2: java.net.URLClassLoader@63947c6b
  
          if (srv_req instanceof Request) {
              System.out.println("DBG: srv_req IS REQUEST");
              req = (Request)srv_req;
              System.out.println("DBG: req class: " + req.getClass().getName());
          } else {
              System.out.println("DBG: srv_req IS NOT REQUEST");
              System.out.println("DBG: srv_req class: " + srv_req.getClass().getName());
              throw new Exception("&lt;expletive deleted&gt;!");
          }
  
          HttpChannel http_channel = req.getHttpChannel();
          EndPoint end_point = http_channel.getEndPoint();
  </code></pre>
  
  <p>What gets output on the console ...</p>
  
  <pre><code>DBG: cl1: WebAppClassLoader{66233253}@3f2a3a5
  DBG: cl2: java.net.URLClassLoader@63947c6b
  DBG: srv_req IS NOT REQUEST
  DBG: srv_req class: org.eclipse.jetty.server.Request
  </code></pre>
  
  <p>Finally the exception gets thrown.</p>
  
  <p>So it seems that <code>instanceof</code> is saying that <code>srv_req</code> is not an instance of
  <code>Reuest</code> because the classloaders are not the same.  The class of <code>srv_req</code>
  is <code>org.eclipse.jetty.server.Request</code>, though.  But in the servlet I cannot
  treat it as such and I cannot cast it.</p>
  
  <p>I've read <a href="https://www.eclipse.org/jetty/documentation/current/jetty-classloading.html" rel="nofollow noreferrer">https://www.eclipse.org/jetty/documentation/current/jetty-classloading.html</a>
  but it's still not clear to me how to get access to <code>srv_req</code> as a
  <code>org.eclipse.jetty.server.Request</code> instance.</p>
  
  <p>I suspect that I am not supposed to be able to do this from the servlet, but it
  can't hurt to ask.</p>
  
  <p>Can I access the <code>javax.servlet.ServletRequest</code>, returned from my
  <code>HttpServletRequestWrapper</code> object, as what it appears to be, an
  <code>org.eclispe.jetty.server.Request</code> instance?</p>
  
  <p>Alternatively, can I create a full duplex NIO channel from what I CAN get access
  to in the servlet?</p>