aspose file tools*
The moose likes Servlets and the fly likes Chat  Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Chat  Servlet " Watch "Chat  Servlet " New topic
Author

Chat Servlet

Roshini Sridharan
Ranch Hand

Joined: Jan 16, 2001
Posts: 143
I am developing a chat application where i want the Servlet to be called whenever there is a message in the Inbox [from the DB].

Any suggestions is highly appreciated. Thanx in advance.

Regards
Roshini
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
interesting concept. Good luck working on it, hope you come back to tell us about your progress?


42
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Roshini,
You are not in a right direction. Servlet is the technology not meant for this kinda stuff. Use socket programming instead. Or you can go with javaspaces too, but it would be complex for you.

and Jeroen,
Its not nice at all.
[ February 02, 2005: Message edited by: Adeel Ansari ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
quite nice I'd say. I encourage him? to go forward with the project and even express interest in what progress is made.
Roshini Sridharan
Ranch Hand

Joined: Jan 16, 2001
Posts: 143
I think the term "Application" has misleaded... I am literally sorry, i want to develop a Web Application for Chat

Hope i could get relevant idea.

Regards
Roshini
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Servlets are "pull" technology. They sit and wait for requests from browsers (or any client that can make a request) and send a response. They don't have the ability to push data down to a browser.

The HTTP protocol is stateless, there is no constant connection from the client to the server so there is no way for a server to notify a client without a request.

Do some Googling on "Applet Chat Apps".
There are plenty open source and freeware projects out there.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
with some hefty Javascript it can be done though. Basically you need to have the browser poll the servlet for data every few seconds.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I actually had an invisible applet working once.
All the applet did was maintain a socket connect to a little server running on the same machine as the webserver. The applet did nothing more than force browser refresh.

Turned out to be more trouble than it was worth
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


with some hefty Javascript it can be done though. Basically you need to have the browser poll the servlet for data every few seconds.

Or you could do it with an HTML META tag. Both are fallible though, since not all browsers support the <META HTTP-EQUIV=Refresh /> tag, and JavaScript can't be guaranteed to work either. Ben's suggestion is your best bet.
[ February 02, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Roshini Sridharan
Ranch Hand

Joined: Jan 16, 2001
Posts: 143
Hi,

Thanks all for the ideas..

I infact thought of an alternate for the meta refresh, as Mr.Ben has suggested i was looking for some way to push information down to the client.

Anyway i will try go forward with meta tag refresh.

Have a Good time.

Regards
Roshini
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
meta refresh is simpler to implement but has the big disadvantage that the entire page is reloaded constantly.
By using JS you can just reload the content of the chatlog without reloading the entire page (possibly deleting whatever the user was typing in the progress).

As already stated, using a pure Java client instead of a browser interface is a cleaner solution.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

...unless you are using frames. (Please don't take that as a recommendation: frames are ugly)

But of course this is a moot point - as others have already suggested, this is not a good way to go. Yet another disadvantage is that in Windows a sound is often tied to the refresh option - so constantly refreshing a page through IE can make your PC tick like a clock.
[ February 03, 2005: Message edited by: Paul Sturrock ]
Roshini Sridharan
Ranch Hand

Joined: Jan 16, 2001
Posts: 143
Its true that the refresh is annoying to the user, i will include the JS to make things better.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Chat Servlet