O'Reilly logo

Killer Game Programming in Java by Andrew Davison

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required