title: Browser can't connect with spring4 websocket server
body: <p>In a working Spring MVC 4.0.5 project running under Tomcat 7.0.50,following the <a href="http://syntx.io/using-websockets-in-java-using-spring-4/" rel="nofollow">the tutorial</a>.</p>
  
  <p>In eclipse console (catalina log) when I start Tomcat, appear the rows</p>
  
  <pre><code>Mapped URL path [/websocket.ws] onto handler of type [class org.springframework.web.socket.server.support.WebSocketHttpRequestHandler]
  </code></pre>
  
  <p>i got error "Firefox can't connect to ws://localhost/Books/websocket.ws" [HTTP/1.1 404 Not Found 6ms] from firefox web console.</p>
  
  <p>My configuration:</p>
  
  <h3>web.xml</h3>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
  
      &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <h3>applicationContext.xml</h3>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:websocket="http://www.springframework.org/schema/websocket"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/websocket
          http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd"&gt;
  
  
      &lt;bean id="websocket" class="com.example.websocket.handler.WebsocketEndPoint" /&gt;
  
      &lt;websocket:handlers&gt;
          &lt;websocket:mapping path="/websocket.ws" handler="websocket" /&gt;
          &lt;websocket:handshake-interceptors&gt;
              &lt;bean class="com.example.websocket.HandshakeInterceptor" /&gt;
          &lt;/websocket:handshake-interceptors&gt;
      &lt;/websocket:handlers&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <h3>WebsocketEndPoint.java</h3>
  
  <pre><code>package com.example.websocket.handler;
  
  import org.springframework.web.socket.TextMessage;
  import org.springframework.web.socket.WebSocketSession;
  import org.springframework.web.socket.handler.TextWebSocketHandler;
  
  public class WebsocketEndPoint extends TextWebSocketHandler{
  
      @Override
      protected void handleTextMessage(WebSocketSession session,
              TextMessage message) throws Exception{
          super.handleTextMessage(session, message);
          System.out.println(message.getPayload());
          TextMessage returnMessage =new TextMessage(message.getPayload()+"received at server");
          session.sendMessage(returnMessage);
      }
  }
  </code></pre>
  
  <h3>HandshakeInterceptor.java</h3>
  
  <pre><code>package com.example.websocket;
  
  import java.util.Map;
  
  import org.springframework.http.server.ServerHttpRequest;
  import org.springframework.http.server.ServerHttpResponse;
  import org.springframework.web.socket.WebSocketHandler;
  import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
  
  public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor{
  
      @Override
      public boolean beforeHandshake(ServerHttpRequest request,
              ServerHttpResponse response,WebSocketHandler wsHandler,
              Map&lt;String,Object&gt; attributes) throws Exception{
          System.out.println("Before Handshake!");
          return super.beforeHandshake(request,response,wsHandler,attributes);
      }
  
      @Override
      public void afterHandshake(ServerHttpRequest request,
              ServerHttpResponse response,WebSocketHandler wsHandler,
              Exception ex){
          System.out.println("After HandShake!");
          super.afterHandshake(request,response,wsHandler,ex);
      }
  }
  </code></pre>
  
  <p>javascript code:</p>
  
  <pre><code>Chat.socket = new WebSocket('ws://127.0.0.1:80/Books/websocket.ws');
  </code></pre>
  
  <p>is the web.xml wrong?</p>
  
  <p>"Books" is my project name in tomcat.</p>
  
  <p>Thanks for any answer.</p>