Java Platform, Enterprise Edition: The Java EE Tutorial

18.6 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.

Close Window

Table of Contents

Java Platform, Enterprise Edition: The Java EE Tutorial

Expand | Collapse