title: Basic Websocket-Example for Jetty 9
body: <p>I'm trying to make a real basic Websocket example work on standalone Jetty, but i'm stuck on a NullPointerException.</p>
  
  <p>Here are my 2 server classes:</p>
  
  <pre class="lang-java prettyprint-override"><code>@WebServlet("/MyWebSocketServlet")
  public class MyWebSocketServlet extends WebSocketServlet {
  
      @Override
      public void configure(WebSocketServletFactory factory) {
          factory.register(MyWebSocket.class);
      }
  }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>@WebSocket
  public class MyWebSocket{
  
      @OnWebSocketConnect
      public void onConnect(Session session){
          System.out.println("MyWebSocket.onConnect()");
      }
  
      @OnWebSocketMessage
      public void onText(String msg) {
          System.out.println("MyWebSocket.onText()");
      }
  
      @OnWebSocketClose
      public void onClose(int statusCode, String reason) {
          System.out.println("MyWebSocket.onClose()");
      } 
  }
  </code></pre>
  
  <p>I try to establish communication through JavaScript in Chrome:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;script type="text/javascript"&gt;
      var echoSocket = new WebSocket(
          "ws://localhost:8080/JettyWebsocketsTest/MyWebSocketServlet"
      );
  
      echoSocket.onopen = function(e) {
          console.log("onopen()");
          echoSocket.send("Hello");
      }
  
      echoSocket.onmessage = function(e) {
          console.log("msg received");
      };
  &lt;/script&gt;
  </code></pre>
  
  <p>Unfortunately the connection won't establish and instead this NullPointerException is thrown:</p>
  
  <pre><code>java.lang.NullPointerException
  at org.eclipse.jetty.websocket.server.WebSocketServerFactory.upgrade(WebSocketServerFactory.java:400)
  at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:168)
  at org.eclipse.jetty.websocket.servlet.WebSocketServlet.service(WebSocketServlet.java:160)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
  at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:558)
  at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:489)
  at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
  at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:520)
  at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:233)
  at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:972)
  at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:417)
  at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:192)
  at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:906)
  at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
  at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
  at org.eclipse.jetty.server.Server.handle(Server.java:350)
  at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:442)
  at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:910)
  at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:565)
  at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:217)
  at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:46)
  at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:545)
  at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:43)
  at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:598)
  at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:533)
  at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>These lines in WebSocketServerFactory throw that Exception:</p>
  
  <pre class="lang-java prettyprint-override"><code>HttpConnection http = HttpConnection.getCurrentConnection();
  EndPoint endp = http.getEndPoint();
  </code></pre>
  
  <p>What can i do for an available HttpConnection?</p>