Clients Using a Servlet as a Server

Figure 30-7 shows the various objects in the servlet-based chat application.

Each client is represented by two objects: a URLChat object manages the GUI and translates user input into messages for the web server. The URLChatWatcher thread periodically queries the server for new messages sent by the other users.

The communication is implemented by sending the server a GET request for the ChatServlet object, with message details added as arguments to the URL. A client is identified by a name and a cookie. The cookie is presented to users when they send a

The servlet-based chat system

Figure 30-7. The servlet-based chat system

hi message to the servlet upon joining the chat system. Most messages require the name and cookie information, which verify the identity of the message sender.

The servlet maintains a synchronized ChatGroup object, which bears many similarities to the ChatGroup object used in the threaded chat application. Client information is held in Chatter objects, while client messages are stored in a messages ArrayList.

A key difference between ChatServlet and the server in the first chat example is that ChatServlet cannot initiate communication with its clients. It must wait for a URLChatWatcher thread to ask for messages before it can send them out.

URLChat transmits its messages as arguments attached to the URL http://localhost:8100/servlet/ChatServlet. The notation ...

Get Killer Game Programming in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.