This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Simple Java Chat online application Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Simple Java Chat online application" Watch "Simple Java Chat online application" New topic
Author

Simple Java Chat online application

Annemarie McKeown
Ranch Hand

Joined: Nov 07, 2003
Posts: 47
Hello,
I am developing a JSP,Servlet webapp, which should include a simple chat room application.
Does anyone know how I would make a start at this, or any online info that would be of help?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
First, is this homework? The gang here is happy to point students in good directions, but is reluctant to provide much code. You'll have more fun figgering it all out on your own. Just let us know where you're coming from.
Second, I think you'll have trouble without adding a little more to your "technology list". With JSP and Servlet there's no way to "push" a message from one user to another. So when I type "Hi!" we can't make the message show on your screen until you click something to ask for new messages. You'll probably want an applet to make the message show up instantly. Applets are moving slightly up the skills list and pushing messages from the server to an applet are a bit further yet. Which either makes them intimidating or just the kind of challenge you want. Let us know if you stopped breathing or jumped on the keyboard to ask for more.
On the other hand, if you're not married to JSP and Servlet, we might steer you to sockets. There's plenty good fun to be had there, too.
So let's get going! Izzit homework or no? What do you think of the technology choices?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Maarten Vergouwen
Ranch Hand

Joined: Jan 12, 2004
Posts: 60
Sun's SL275 course culminates in building a chat application; I'm sure you could find the source files somewhere of someone who has done the course (perhaps even Sun).
It's not exactly what you want (doesnt use servlets), but you could take it as a starting point perhaps.
[ March 03, 2004: Message edited by: Maarten Vergouwen ]
Annemarie McKeown
Ranch Hand

Joined: Nov 07, 2003
Posts: 47
Thanks for your help, nope not homework.
I have done something similar before using sockets, just wondering if there was a way of writing servlets to handle chat.
I'll go in the applet direction.
Thanks again.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm using a vendor framework at work that has an applet they call "network presence" It allows the server to push events to the browser so they can be handled in JavaScript. I think the applet (acting as server) opens a ServerSocket and the server (now a client, just to be confusing) keeps a list of machine ort addresses that it believes to be logged on. The app has an "availability" switch on the UI that controls a little session object on the server so the server knows who is accepting messages. The vendor uses it to push all kinds of events to the browser. Pretty cool.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
You can use servlets, and in fact it's been done I think.
What you need is a client that refreshes itself every second or so (maybe every few seconds if you don't mind having to wait a bit to see new lines appear).
Whenever you press enter after typing something the text is sent to the servlet and stored in a memory buffer.
When a client request the chat data that buffer is returned from the last point that client got the data the last time.
The client is responsible for caching the data it had retrieved in the past (to prevent heaps of network traffic and using loads of server resources).
The client request for data can be handled through sending HTTP requests from Javascript, which can then append the data to the content of any HTML control (this will require some pretty hefty Javascript coding, it's advanced stuff but not that long).
The servlet can keep track of which data has been sent to which client based on the session ID (say, keep a Map of session IDs mapped to the index in a List of entries).
When every client connected at the time it was posted has retrieved the first entry in the List it is deleted and all indices are updated to reflect this.
You might want to store the entry for logging purposes in a textfile or database, but if you do there may be legal challenges (privacy issues) to take into consideration.


42
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Simple Java Chat online application
 
Similar Threads
Technologies for a Web based chat application
Chat Applets
online easy servers for use?
Chat Application
Proj: Chatting Application