Chapter 5. Chatting on the Internet with the Chat Room
The Chat Room.
How about an Internet chat room? This project, Chat, is your own private chat room, and it'll keep you in touch with whomever you want to stay in touch withall you need is Internet access and a Java-enabled web server. Talk all you likeall you're paying for is the local Internet connection, which is often free in hotels these days. You can also use Chat to set up your own chat rooms, dedicated to your own interests, on your own website. Among other things, you can avoid long-distance phone bills this wayI use a variant of this application to stay in touch with home when I'm on the road.
How does someone enter the chat room? It's easy. The user simply enters the URL for chat.html in the browser. You can see what it looks like in Figure 5.1other people only need to go to the same URL, enter their name in the text field (the default is "Guest"), type their comments, and click the Submit button. Their comments will show up in the top part of the page, which is refreshed every 5 seconds (you can alter the refresh rate). And the whole conversation takes off.
Figure 5.1. The chat room at work.
That's an HTML page in Figure 5.1. So where does Java come into this? There are two files needed for this projectchat.html and chat.jsp. The second file, chat.jsp, is where the real action is. This is a JavaServer Pages (JSP) file, and you're going to see how to create it in this chapter. JSP files can contain both normal Java code and HTML, in the same file, which is perfect for working online.
To use Java online, you're going to need a web server that supports JavaServer Pages. Many web servers do support JSP these days, so ask your ISP whether its servers do. If not, it's easy to find a web server that doesjust search for "jsp host" on the Internet. You'll find hundreds.
The first file, chat.html, provides the framework that displays the output of chat.jsp.