Using WebSocket under Tomcat

Environmental Science

  • tomcat 9
  • JDK8
  • web project with framework


Jar package selection

Because the packages used in the online tutorials are many and miscellaneous, the average person may not see how to use them. In fact, the choice is very simple
  • J2EE: Java EE API with J2EE
  • Others: javax.websocket
  • tomcat: built in websocket API
In fact, the other two packages encapsulate javax.websocket. Because tomcat is used here, tomcat is directly used. The source code behind is from the network

Front end source code

var ws = null;
if('WebSocket' in window) {
    ws = new WebSocket("ws://localhost:8080/xx/websocket");
    ws.onopen = function(){
    ws.onmessage = function(evt){
        var received_msg =;
    ws.onclose = function(){
        alert("Connection closed..."); 
    ws.onerror = function(){
        alert("An error occurred");

Back end code

import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

public class WebsocketTest {
	//Static variable, used to record the current number of online connections.

    private static AtomicInteger onlineCount = new AtomicInteger(0);

    //The thread safe Set of the concurrent package is used to store the MyWebSocket object corresponding to each client. To realize the communication between the server and a single client, Map can be used for storage, where Key can be the user ID

    protected static CopyOnWriteArraySet<WebsocketTest> webSocketSet = new CopyOnWriteArraySet<WebsocketTest>();

    //A connection session with a client through which data is sent to the client

    private Session session;

    public WebsocketTest() {
        System.out.println(" WebSocket init~~~");
     * Method successfully called for connection establishment
     * @param session  Optional parameters. Session is a connection session with a client, through which data is sent to the client
    public void onOpen(Session session){
        this.session = session;
        webSocketSet.add(this);     //Add to set

        addOnlineCount();           //Online data plus 1

        System.out.println("New connection to join! The current number of people online is" + getOnlineCount());

     * Method of connection close call
    public void onClose(){
        webSocketSet.remove(this);  //Remove from set

        subOnlineCount();           //Online number minus 1

        System.out.println("There is a connection closed! The current number of people online is" + getOnlineCount());

     * Method of calling after receiving client message
     * @param message Messages sent by clients
     * @param session Optional parameters
    public void onMessage(String message, Session session) {
        System.out.println("Messages from clients:" + message);
        //Mass message

        for(WebsocketTest item: webSocketSet){
            try {
            } catch (IOException e) {

     * Called when an error occurs
     * @param session
     * @param error
    public void onError(Session session, Throwable error){
        System.out.println("An error occurred");

     * This method is different from the above methods. Without annotation, it is a method added according to your own needs.
     * @param message
     * @throws IOException
    public void sendMessage(String message) throws IOException{


    public static int getOnlineCount() {
        return onlineCount.get();

    public static void addOnlineCount() {

    public static void subOnlineCount() {


Problem analysis

404 error

  • Jar package was not called successfully: tomcat's websocket API package can be configured to Build Path as an external package, and only one websocket package can be used
  • Path intercepted: for example, if the framework intercepts the path automatically, first release the corresponding websocket path rule, and then note that the full path is the localhost:8080 / project name / websocket annotation path

302 error

  • The backend is not defined correctly, and the annotation is not fully effective

Introduction of design pattern

  • It can be simply divided into websocket class and session manager singleton class for managing session, which conforms to the principle of single responsibility
  • It's complex, and it can take the form of an observer model

Keywords: Session Tomcat Java network

Added by mbh23 on Sun, 03 May 2020 09:57:12 +0300