importjavax.websocket.*;importjavax.websocket.server.PathParam;importjavax.websocket.server.ServerEndpoint;importjava.io.IOException;importjava.util.Set;@ServerEndpoint(value="/websocket/chat/{clientId}")publicclassChatAnnotation{privatestaticfinalSet<ChatAnnotation>connections=newCopyOnWriteArraySet<>();privateStringnickname;privateSessionsession;@OnOpenpublicvoidstart(@PathParam("clientId")StringclientId,Sessionsession){this.nickname=clientId;this.session=session;connections.add(this);}@OnClosepublicvoidend(){connections.remove(this);}@OnMessagepublicvoidincoming(Stringmessage){// Never trust the clientStringfilteredMessage=String.format("%s: %s",nickname,message.toString());broadcast(filteredMessage);}@OnErrorpublicvoidonError(Throwablet)throwsThrowable{//log.error("Chat Error: " + t.toString(), t);}privatestaticvoidbroadcast(Stringmsg){for(ChatAnnotationclient:connections){try{synchronized(client){Stringmessage="{clId:'"+client.nickname+"',msg:'"+msg+"'}";client.session.getBasicRemote().sendText(filter(message));}}catch(IOExceptione){connections.remove(client);try{client.session.close();}catch(IOExceptione1){// Ignore}Stringmessage=String.format("* %s %s",client.nickname,"has been disconnected.");broadcast(message);}}}privatestaticStringfilter(Stringmessage){if(message==null)return(null);charcontent[]=newchar[message.length()];message.getChars(0,message.length(),content,0);StringBuilderresult=newStringBuilder(content.length+50);for(inti=0;i<content.length;i++){switch(content[i]){case'<':result.append("<");break;case'>':result.append(">");break;case'&':result.append("&");break;case'"':result.append(""");break;default:result.append(content[i]);}}return(result.toString());}}
jsp page
<%@ page language="java" contentType="text/html; charset=UTF-8"%><style type="text/css">.scrollup{position:fixed;bottom:0px;right:0px;z-index:99999;text-align:right;padding-right:10px;}.chat{list-style:none;margin:0;padding:0;}.chatli{margin-bottom:10px;padding-bottom:5px;border-bottom:1pxdotted#B3A9A9;}.chatli.left.chat-body{margin-left:60px;}.chatli.right.chat-body{margin-right:60px;}.chatli.chat-bodyp{margin:0;color:#777777;}.panel.slidedown.glyphicon,.chat.glyphicon{margin-right:5px;}.panel-body{overflow-y:scroll;height:250px;}::-webkit-scrollbar-track{-webkit-box-shadow:inset006pxrgba(0,0,0,0.3);background-color:#F5F5F5;}::-webkit-scrollbar{width:12px;background-color:#F5F5F5;}::-webkit-scrollbar-thumb{-webkit-box-shadow:inset006pxrgba(0,0,0,.3);background-color:#555;}</style><script type="text/javascript">varchat={};chat.socket=null;chat.connect=(function(host){if('WebSocket'inwindow){chat.socket=newWebSocket(host);}elseif('MozWebSocket'inwindow){chat.socket=newMozWebSocket(host);}else{chatContent.append('Error: WebSocket is not supported by this browser.','left');return;}chat.socket.onopen=function(){chatContent.append('Info: WebSocket connection opened.','left');document.getElementById('chat').onkeydown=function(event){if(event.keyCode==13){chat.sendMessage();}};};chat.socket.onclose=function(){document.getElementById('chat').onkeydown=null;chatContent.append('Info: WebSocket closed.','left');};chat.socket.onmessage=function(message){chatContent.append(message.data,'right');};});chat.initialize=function(){if(window.location.protocol=='http:'){//console.log(window.location.host);chat.connect('ws://'+window.location.host+'/websocket/chat/${login_empnm}');}else{chat.connect('wss://'+window.location.host+'/websocket/chat/${login_empnm}');}};chat.sendMessage=(function(){varmessage=document.getElementById('chat').value;if(message!=''){chat.socket.send(message);document.getElementById('chat').value='';}});varchatContent={};chatContent.append=(function(message,addClassNm){varchatContainer=document.getElementById('chat_li');varli=document.createElement('li');varspan=document.createElement('span');span.style.wordWrap='break-word';console.log("msg : "+message);span.innerHTML=message;span.className="pull-"+addClassNmli.appendChild(span);li.className="clearfix ";li.className=li.className+""+addClassNm;chatContainer.appendChild(li);document.getElementById('chat_body').scrollTop=document.getElementById('chat_body').scrollHeight;while(chatContainer.childNodes.length>25){chatContainer.removeChild(chatContainer.firstChild);}});chat.initialize();functionchkMyMsg(msgJson){}</script><div><p></p><divid="console-container"><divid="console"></div></div></div><divclass="scrollup span4"><divclass="row-fluid"><divclass="box dark"><header><h5>Chat</h5><divclass="toolTip"><aclass="btn btn-primary btn-small"data-toggle="collapse"data-parent="#accordion"href="#collapseOne"style="margin-top: 5px; margin-right: 2px;">
Open
</a></div></header><divclass="body collapse"id="collapseOne"><divclass="panel-body"id="chat_body"><ulid="chat_li"class="chat"></ul></div><div><divclass="input-group"><inputtype="text"placeholder="type and press enter to chat"id="chat"><spanclass="input-group-btn"><buttonclass="btn btn-warning btn-sm"id="btn-chat">
Send</button></span></div></div></div></div></div></div>