Java Platform, Enterprise Edition (Java EE) 8
The Java EE Tutorial

Previous Next Contents

Maintaining Client State

Because the container creates an instance of the endpoint class for every connection, you can define and use instance variables to store client state information. In addition, the Session.getUserProperties method provides a modifiable map to store user properties. For example, the following endpoint replies to incoming text messages with the contents of the previous message from each client:

public class DelayedEchoEndpoint {
   public void open(Session session) {
      session.getUserProperties().put("previousMsg", " ");
   public void message(Session session, String msg) {
      String prev = (String) session.getUserProperties()
      session.getUserProperties().put("previousMsg", msg);
      try {
      } catch (IOException e) { ... }

To store information common to all connected clients, you can use class (static) variables; however, you are responsible for ensuring thread-safe access to them.

Previous Next Contents
Oracle Logo  Copyright © 2017, Oracle and/or its affiliates. All rights reserved.