title: Apache Tomcat websockets implementation on localhost
body: <p>I am trying to learn websockets. Tried a few examples given on apache tomcat 7 websockets examples -&gt; 'http://tomcat.apache.org/tomcat-7.0-doc/web-socket-howto.html'. I'm done with the client-side code. having a problem with the server-side one which is to be written in a java servlet. I tried to run the examples on my localhost using eclipse and Apache Tomcat 7.0.52.
  Can anyone help me with some tutorials and examples for the server code.</p>
  <p>Here is the client side code for a simple echo server:</p>
  <pre><code>var wsUri = &quot;ws://echo.websocket.org/&quot;; 
  var output; 
  
  function init() { 
  output = document.getElementById(&quot;output&quot;);
  $('‪#‎send‬').click(function(){
  doSend(&quot;ja ghari&quot;);
  });
  testWebSocket(); } 
  
  function testWebSocket() {
  websocket = new WebSocket(wsUri); 
  websocket.onopen = function(evt) { onOpen(evt);};
  websocket.onclose = function(evt) { onClose(evt) ;}; 
  websocket.onmessage = function(evt) { onMessage(evt) ;}; 
  websocket.onerror = function(evt) { onError(evt) ;}; } 
  
  function onOpen(evt) { 
  writeToScreen(&quot;CONNECTED&quot;); 
  doSend(&quot;WebSocket rocks&quot;);
  } 
  
  function onClose(evt) { 
  writeToScreen(&quot;DISCONNECTED&quot;); } 
  
  function onMessage(evt) { 
  writeToScreen('&lt;span style=&quot;color: blue;&quot;&gt;RESPONSE: ' + evt.data+'&lt;/span&gt;'); 
  } 
  
  function onError(evt) { 
  writeToScreen('&lt;span style=&quot;color: red;&quot;&gt;ERROR:&lt;/span&gt; ' + evt.data); } 
  
  function doSend(message) { 
  writeToScreen(&quot;SENT: &quot; + message); 
  websocket.send(message); } 
  
  function writeToScreen(message) { 
  var pre = document.createElement(&quot;p&quot;); pre.style.wordWrap = &quot;break-word&quot;; 
  pre.innerHTML = message; output.appendChild(pre); } 
  
  window.addEventListener(&quot;load&quot;, init, false); 
  </code></pre>
  <p>now i need to write a server side code in java for this
  I need a server which will communicate with my client using websockets</p>
  <p>Apache Tomcat 7 or above is a must, server side code to be written in java.
  Thanks in advance.</p>