title: Tomcat websocket is not working
body: <p>I have tried a websocket sample code as below, my browser is supporting HTML 5 websocket, but the sample code below always prompt "Close" in the javascript. What happen to the code?</p>
  
  <p><strong>websocket.java</strong></p>
  
  <pre><code> @WebServlet("/websocket")
      public class websocket extends WebSocketServlet {
          private static final long serialVersionUID = 1L;
  
          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              response.getWriter().println("welcome to websocket 2");
              response.getWriter().flush();   
      }
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
      }
  
      @Override
      protected StreamInbound createWebSocketInbound(String arg0,
              HttpServletRequest arg1) {
  
          return new TheWebSocket();
      }
  
      private class TheWebSocket extends MessageInbound
      {
          private WsOutbound outbound;
  
          @Override
          public void onOpen( WsOutbound outbound )
          {
              this.outbound = outbound;
               System.out.println("socket opened!");
          }
  
          @Override
          public void onTextMessage( CharBuffer buffer ) throws IOException
          {
              try
              {
                      outbound.writeTextMessage( CharBuffer.wrap( "abc testing".toCharArray() ) );
                      System.out.println("Message sent from server.");
              }
              catch ( IOException ioException )
              {
                      System.out.println("error opening websocket");
              }
  
          }
  
          @Override
          protected void onBinaryMessage(ByteBuffer arg0) throws IOException {
              // TODO Auto-generated method stub
  
          }
      }
  
  }
  </code></pre>
  
  <p><strong>index.jsp</strong></p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=UTF-8"  
      pageEncoding="UTF-8"%&gt;  
  &lt;!DOCTYPE html&gt;  
  &lt;html&gt;  
  &lt;head&gt;  
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;  
  &lt;title&gt;Index&lt;/title&gt;  
  &lt;script type="text/javascript"&gt;  
  var ws = null;  
  function startWebSocket() {  
      if ('WebSocket' in window)  
          ws = new WebSocket("ws://localhost:8080/web_test/websocket");  
      else if ('MozWebSocket' in window)  
          ws = new MozWebSocket("ws://localhost:8080/web_test/websocket");  
      else  
          alert("not support");  
  
  
      ws.onmessage = function(evt) {  
          alert(evt.data);  
      };  
  
      ws.onclose = function(evt) {  
          alert("close");  
      };  
  
      ws.onopen = function(evt) {  
          alert("open");  
      };  
  }  
  
  function sendMsg() {  
      ws.send(document.getElementById('writeMsg').value);  
  }  
  &lt;/script&gt;  
  &lt;/head&gt;  
  &lt;body onload="startWebSocket();"&gt;  
  &lt;input type="text" id="writeMsg"&gt;&lt;/input&gt;  
  &lt;input type="button" value="send" onclick="sendMsg()"&gt;&lt;/input&gt;  
  &lt;/body&gt;  
  &lt;/html&gt;  
  </code></pre>
  
  <p>When I connect to "http://localhost:8080/web_test/websocket", I got correct message which is "welcome to websocket 2". And my index.jsp file is in the root directory after web_test. So, my deployment should be fine, but somewhere is wrong. I just cannot figure it out.</p>