aspose file tools*
The moose likes Servlets and the fly likes servlet to another client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet to another client" Watch "servlet to another client" New topic
Author

servlet to another client

Jignesh Gohel
Ranch Hand

Joined: Dec 28, 2004
Posts: 276
Hello, I am making a chat application in that my client is in JAVA SWING and server side there is a servlet...

Now, i have passes the message from ClientA to servlet and i am successfully getting that message on the servlet. for that i have done the following..

Client Server
---------------------------------------
DataOutputStream Inputstream

Now, I want to pass that message from servlet to another client who is already in connection with this servlet. If i must use the stream then which are they i mean server side and client side to pass the messages....

So, help me to pass the message from servlet to my another client who is in SWING..

Waiting for your reply,
Thanks in advance,
Siddharth


Regards,
Jignesh

The Art Of Life Is To Know When To Be Useless And When To Be Useful - CHUANG TZU
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Client can check the server for its new messages. Keep on requesting server, say after every two seconds.

"Pushlets" might be helpful.

Chatting application using servlet is not a good idea, though. Better use, applet over sockets.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

HTTP is not a statefull protocol. The app server has no idea where client B is or if it even exists. In other words, you can't push data from servlet.

Client B will either need to constantly poll the server for new messages (which will gobble up lots of bandwidth and not make for a very responsive user experience) or you will need to do some socket programming to maintain a statefull connection from the server to the clients.

The second would be my choice. Of course once you do that, there will be no need for a servlet app which means this forum will be of little use to you.

Good-Luck
-Ben

PS: There are quite a few share-ware and open source chat apps written in Java. You could probably find one and modify it to suit your needs pretty easily.


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

Joined: Dec 28, 2004
Posts: 276
Hello,

Thanks for the reply.

You are telling that Chat Application using servlets is not a good idea.
But in the definition of our project we are told not to use Socket Programming.It should be done using Http only.So could you please clarify which technology we should use to make this Chat Application in Java & infact if possible we have to make it same like Yahoo Messenger.So tahts why we are using Swing frames on teh client side.

Please reply me soon.

Thanking you,
Jignesh
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Polling is not that big a drain on the system, once every 2 seconds is plenty for a chat system but would be slow for a shooter game. If you build your chat system right, a GET to see if anything has changed will only take a few milliseconds, so one hundred chatters would be no big deal.


Java Resources at www.wbrogden.com
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Jignesh Gohel:
Hello,

Thanks for the reply.

You are telling that Chat Application using servlets is not a good idea.
But in the definition of our project we are told not to use Socket Programming.It should be done using Http only.So could you please clarify which technology we should use to make this Chat Application in Java & infact if possible we have to make it same like Yahoo Messenger.So tahts why we are using Swing frames on teh client side.

Please reply me soon.

Thanking you,
Jignesh




I'm saying it's not the best technology for the task. If your project requires it, then you client will have to poll.
As William said, use GET to keep the payload small will help.

To add to that:
Learn about the "If-Modified-Since" request header and the "Date" response headers. With them you will be able to minimize the size of the response to:

Jignesh Gohel
Ranch Hand

Joined: Dec 28, 2004
Posts: 276
Thank you

Though it is not a good way to build it using servlets then also if i want to do it using the same then what would be solution and how to proceed further. also i have some questions reg servlets::

1. how to build threaded servlet.
2. how to modularize the servlet for the purpose
3. if communication needs to be between multiple clients then how the servlet would handle the streams for the data transfer
4. what type of code would be there in servlets run() method.
5. how to maintain the thread between two clients when there are multiple clients communicating via chat.

Thanks in advance
Jignesh
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

1. how to build threaded servlet.
Servlets are already threaded.

2. how to modularize the servlet for the purpose
You do this by putting all of you functionality in beans and calling their methods from the servlet.

3. if communication needs to be between multiple clients then how the servlet would handle the streams for the data transfer
Again, servlets are not designed to handle statefull protocols.
A client makes a request. The servlet resonds and the connection is closed.

4. what type of code would be there in servlets run() method.
Servlets don't have a run method.

5. how to maintain the thread between two clients when there are multiple clients communicating via chat.
See the answer to #3.
I would have all requests read and write to a common object or database.
If persistance isn't an issue, you could create a "Context Scoped" object with a key for every user that is logged in (their session ID would make a nice key).
Jignesh Gohel
Ranch Hand

Joined: Dec 28, 2004
Posts: 276
Thank You, Sir
But I still have some doubt. In answer no.4 you have told that servlet does not have run() method... I mean i don't understand this.. like if the the servlet does not have run() then how can I able to mangae all the requests and response from the clients for ex. suppose there are 100 clients then there must be 100 requests and 100 response... isn't it?

If any post or articles regarding this then please give me the reference...

Thanks in advance,
Jignesh
Jignesh Gohel
Ranch Hand

Joined: Dec 28, 2004
Posts: 276
Thank You, Sir
But I still have some doubt. In answer no.4 you have told that servlet does not have run() method... I mean i don't understand this.. like if the the servlet does not have run() then how can I able to mangae all the requests and response from the clients for ex. suppose there are 100 clients then there must be 100 requests and 100 response... isn't it?

If any post or articles or any example of multithreading application regarding this then please give me the reference...

Thanks in advance,
Jignesh
anupa oru
Ranch Hand

Joined: Jan 15, 2005
Posts: 118
Hi,
Sorry for interrupting in between please forgive me for this.You asked if there is no run in servlet then how mulitiple client requests will ne handled.It's the responsiblity of Servlet container to handle multiple clients1. it may maintain pool of servlet instances Or 2.it may synchronize multiple threads access to servlet .If you want your servlet container to maintain pool of instances then you may implement SingleThreadModel marker interface.I hope this is correct.Correct me if I am wrong.
Thanks in advance
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The programmer does not manage the threads in a servlet app.
As Anupa has correctly stated, the container manages threading for you.
You just put your code in one of the service methods (doGet/doPost) and understand that each request will be handled in it's own thread.

Single threaded model has been deprecated for years. Don't start using it now.

There will be one servlet instance per JVM with a new thread for each request.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Maybe you should start with a beginners' book on servlets, or maybe a beginners' book on Java.

The only correction I need to make on Ben's wise words is that Servlets CAN be used to represent stateful protocols.
It's just never done in practice as in practice Servlets are used exclusively (or almost) to handle HTTP which is a stateless protocol.
In theory at least one could implement a Servlet engine to handle a stateful protocol.


42
Jignesh Gohel
Ranch Hand

Joined: Dec 28, 2004
Posts: 276
Thanx for giving me this reply.
As per you Mr Souther's reply that servlet is not as good to use for making a chat application.
I would like to ask Sir that is there anything other i could rely upon so that the messages sent may be received by the client without any delay

Give a expertise suggestion of urs to make a chat app that is http no socket progg.

Plz help me out Waitin 4 ur replies.

Regards
Jignesh
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Ok, let's understand what you're trying to do...

1. Write a rich client using Swing for a chat application.
2. The server's implementation must not use sockets and must be servlet based.

Is this pretty much it? Ok. What should your chat application allow a user to do...

1. Send messages to other users in the same conversation (could be one-on-one or a "chat room").
2. See messages sent to them from the other users in the same conversation.

Right? I can see maybe two servlets (could be one actually) in your application which perform the following operations...

1. A servlet that receives messages sent from a client and stores them on the server somewhere (Mr. Souther had a good idea about a context-scoped variable of some sort, maybe call it UserMailbox or something).
2. A servlet that returns all new messages for a given user/conversation.

That's a pretty big hint and should get you started (provided you've ever coded a servlet before). Of course, there's the idea of establishing the conversation(s) in the first place, but if you can get this part down, that should be easy. Good luck.


James Carman, President<br />Carman Consulting, Inc.
Jignesh Gohel
Ranch Hand

Joined: Dec 28, 2004
Posts: 276
Hello all
Thanks for ur replies.
Now i think i hav a good idea of what I m going to do.
1 question that would i need to store the msgs within the servlet or else i should make a db table to store the msg against the id of the person
Or storing within a servlet would be a better idea but would it cause complexities??
Last but not the least when the user signs in should i maintain the online names using the db or i should go for ldap as stated to me by someone
Any good tips from you all experts would let us go ahead fast
Thanks
Regards
Jignesh
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I wouldn't store anything in the servlet. Either use a database, a context scoped object, or a singleton object (we'll call it postOffice to go with James Carmen's theme).

When a user logs in, I would add their account to the postOffice. Their session ID would make a good key. I would use a session listener to remove their account from the mailbox when their session is destroyed (or mark them as off-line if you are using a database).

When person A sends a message to person B, the servlet will check to see if person B is still logged in and write the message to thier mailBox within the post office.

Your polling servlet would constantly check to see who's currently logged in and check to see if it's user has any new messages.

The clever use of frames, javascript, and the HTTP headers listed above could help avoid the need to download the entire page with each iteration of the polling servlet -- saving some bandwidth.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
I question that would i need to store the msgs within the servlet or else i should make a db table to store the msg against the id of the person

Unless you need to archive the messages in a searchable form, using a database is overkill. If all you want is a list of messages for a particular chat "room" in chronological order, one of the Java collections is fine - when it gets too large, just remove the oldest messages. Compared to the memory capacity of a modern PC, chat messages are tiny.
One of my pet peeves in programming is the tendency to use a database when a simple data structure will do. "As simple as possible but no simpler."
Bill
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by Ben Souther:
Either use a database, a context scoped object, or a singleton object (we'll call it postOffice to go with James Carmen's theme).


Exactly what I would have called it, Ben! That sounds like a logical analogy to me.
Jignesh Gohel
Ranch Hand

Joined: Dec 28, 2004
Posts: 276
Hello all
Thanks once again for ur replies.
Sir first of all we dont need to store the offline msgs we need just send the messages when the users are online thats it along with file transfer

So what would be the better option to go for if the msg is to be send directly
and no future storage required.

And one thing that we are using Swing at the client side

In this we face a problem that when we are sending the message from client A to the servlet it is received . but how to make the opp client to receive the same message from the servlet.

How to make it happen ?? Is swing a proper selection or we should opt for anything else for convenience in communication with the servlets.

Thanks
Regards
Jignesh
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Sir first of all we dont need to store the offline msgs we need just send the messages when the users are online thats it along with file transfer
So what would be the better option to go for if the msg is to be send directly
and no future storage required.
Then go with what we've been talking about and use an "in memory" data structure to hold the messages.

And one thing that we are using Swing at the client side
Fine choice, you'll have a lot of options with Java on both the client and server.

In this we face a problem that when we are sending the message from client A to the servlet it is received . but how to make the opp client to receive the same message from the servlet.
Again, without (as Jeroen Wenting has stated) re-engineering a servlet to implement a stateful protocol, you can't send anything. In a stateless environment, (which HTTP is) your servlet will sit and wait for a request and then respond to it. Each client will have to keep asking the server "are there any messages for me?".

How to make it happen??
Nobody's going to write it for you....
Well...
Maybe someone already has
http://www.google.com/search?hl=en&q=Servlet+Chat+App&btnG=Google+Search

Is swing a proper selection or we should opt for anything else for convenience in communication with the servlets.
Again, Swing is a fine choice. You could also write it for a browser using HTML and javascript.
Jignesh Gohel
Ranch Hand

Joined: Dec 28, 2004
Posts: 276
hello,

Thank you all for your great support & your expert suggestions.

Now sir ,I would like to ask you that if our client is in pure swing that's a frame then is it possible to submit the parameters to the servlet like we are doing when our client is in html language & if possible then how to receive the parameters & their values in the servlet.

We checked out the link given given by you but most of the clients were made in the JSP which used HTML so it was easy to get the parameters in the servlet using getParameter().

So if the thing above mentioned by me ,is possible then how to implement it.
Some guidelins regarding that please.

Thanking you,
Jignesh
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Yes, look at java.net.UrlConnection:
http://java.sun.com/j2se/1.4.2/docs/api/java/net/URLConnection.html

You might also want to read up on Apache's HttpClient classes
http://jakarta.apache.org/commons/httpclient

But the best thing about having pure Java on both ends is that you can stream java objects back and forth from the server to the client and vica versa:
http://www-128.ibm.com/developerworks/java/library/j-tunnel/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet to another client
 
Similar Threads
help needed
how to transfer a file from client to server???
How do you implement Web Services security?
About Updating JAR FILES
URL query