This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes Sending response to client with out receving request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sending response to client with out receving request" Watch "Sending response to client with out receving request" New topic
Author

Sending response to client with out receving request

Mohan Karthick
Ranch Hand

Joined: Apr 11, 2005
Posts: 199
Is it possible to send a notification on the browser after user logged in for about 5 minutes, like "U have new e mail.". If we are working on servlet based HTTP request/response system.

The basic catch is how can track this 5 minute and send this notification , may be URL redirect or something else, note that we are doing that with receiving request from client.

Thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

A servlet can't push anything to the client without a request.

This is usually done by having the client make a request every n seconds. If you're using frames, you can have one of the frames do the polling via a meta-refresh tag. The frame can also be a hidden iframe so the page won't look like a framed website.

Alternately, look up the Javascript XMLHttpRequest object. This allows you to make an HTTP request from Javascript without refreshing the whole page.
[ May 03, 2005: Message edited by: Ben Souther ]

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

Joined: Apr 11, 2005
Posts: 199
If we embbed a applet as a JMS client listener and on server side start a thread which will call a different method(logic of sending messgae to JMS provider after 5 minute) mean while servlet's service method will continue doing rest of the reguest response process. After 5 min when method send message to JMS provider it will be listened by client applet and will take some action or force user to do appropriate action by showing dialog box or some thing.
Is this architecture is good ?

Pls advice.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Yes we can use applet over a socket or JMS to achieve this kinda mechanism.
Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149
Originally posted by Mohan Karthick:
If we embbed a applet as a JMS client listener and on server side start a thread which will call a different method(logic of sending messgae to JMS provider after 5 minute)

Is this architecture is good ?

Pls advice.


Overly complicated in my view.

There is no way to have a push under the given circumstances.

What you propose is only an overly complicated way to replace request-response with request-otherrequest-otherresponse-response

As mentioned before, if you want to implement the initial requirements you should go with the usual HTTP Redirect. It has its own flaws, but it works - even behind firewalls and proxies - something the applet thing certainly won't do without problems.

J.
Mohan Karthick
Ranch Hand

Joined: Apr 11, 2005
Posts: 199

-----------------------------
As mentioned before, if you want to implement the initial requirements you should go with the usual HTTP Redirect. It has its own flaws, but it works - even behind firewalls and proxies - something the applet thing certainly won't do without problems.
-----------------------------------------------------
J


I have not understood what you suggested.
Suppose i am maintaing a simple session with user, and after 5 minutes of log in i want to send a message from server. That is only possible if on client side some client code is runing..so that is applet, becaue this 5 minutes cal calculation is from server side so I don't want to do it using java script.

Suppose he is browsing list of item and selected few items, mean while database get updated with two more item, now i want that user must refresh his page so that he can see the upadated items, but certainly wants to keep those item selected which user selected earlier.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
there is a meta-refresh HTTP header, which when set, keeps refreshing the HTML page after the time specified in the header elapses. so you can set that header to keep refreshing after every 5 minutes.

This cant be a dynamic update


Suppose he is browsing list of item and selected few items, mean while database get updated with two more item, now i want that user must refresh his page so that he can see the upadated items, but certainly wants to keep those item selected which user selected earlier.


every time the page 'refreshes' it sends all the info to the servlet, which can send the checked items back as they are AND add the new items.
but, the client will get the updated list only AFTER the specified time has elapsed and the client has connected to the servlet again for new info.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Suppose he is browsing list of item and selected few items, mean while database get updated with two more item, now i want that user must refresh his page so that he can see the upadated items, but certainly wants to keep those item selected which user selected earlier.


You want to watch out not to annoy your customer when you do this.
If I were browsing products and in the process of adding something to a cart or in the middle of reading a paragraph describing one product and the page just refreshed itself, on it's own, causing me to loose my place, I might get frustrated and leave.
A popup or an icon that changes color (dare I say blinks) to indicate that there is more information availible for this page might be more palataable.
Mohan Karthick
Ranch Hand

Joined: Apr 11, 2005
Posts: 199
Ok Ben your right, well i didn't exactly mean to refresh the page all of sudden. My point is how can I tell client, like some thing happened at server for example it could be notification for mail !!

Neraj thanks for your input but i don't want to control it from client side because i am not sure whether the update at DB is every 5 minute or in 48 hours, basic thing is when ever some update happens on server then only I want to notify my client.(using servlet/JSP)

So my conclusion is Applet JMS provider is best solutions, pls let me know the flaw of my solution.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I agree that the applet is probably a heavy handed approach to this.
You'll have to consider what to do for people who don't have the Java plugin to run it.

A polling frame or a Javascript function that checks with the server every n seconds via the XMLHttpRequest control would seem to be a lot easier and robust to me -- especially for a public, internet site.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Also, as Jeffery has mentioned.
Relying on communication through any ports other than 80 and 443 leaves you open to firewall issues -- both on the server and the client side.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
i don't want to control it from client side because i am not sure whether the update at DB is every 5 minute or in 48 hours

You should rethink that requirement - it will be MUCH simpler to have the client side do a request every X seconds to see if there is anything new. On the server side your architecture should be organized around making it quick to determine if this user has any new data.
Either an applet or JavaScript can be used, I would lean toward JavaScript - in either case it does NOT have to redo the page in order to show that new information is available. Just change the contents of a single element of the page.
Bill
Mohan Karthick
Ranch Hand

Joined: Apr 11, 2005
Posts: 199
I am really thankful to all of your for your inputs.

Suppose instead of applet I use jsp include tag and put it at the bottom for notification purpose, now this jsp at the footer actually open a connection to JMS provider/ observer pattern and listening it, when ever JMS provider recv a notificationits update all JMS client(JSP include JSP file) listen it take for respected action, this way client can see the action which he needs to do.

What about this ? well I am avoiding java script for any purpose because i noticed that the developmwnt time increases and need to test it very extensively becaue java script error is really very irritating, and we are expecting that client browser is going to be in variation.

We use to give stress on all server side control.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
If you think debugging javascript is annoying, how do you expect to debug/test JMS?

the functionality that we are talking about here is very basic and will be supported by most browsers available today.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Mohan Karthick:
I am really thankful to all of your for your inputs.

Suppose instead of applet I use jsp include tag and put it at the bottom for notification purpose, now this jsp at the footer actually open a connection to JMS provider/ observer pattern and listening it, when ever JMS provider recv a notificationits update all JMS client(JSP include JSP file) listen it take for respected action, this way client can see the action which he needs to do.

What about this ? well I am avoiding java script for any purpose because i noticed that the developmwnt time increases and need to test it very extensively becaue java script error is really very irritating, and we are expecting that client browser is going to be in variation.
We use to give stress on all server side control.


JSP is server side, whether it's in an include tag or not.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
Suppose instead of applet I use jsp include tag and put it at the bottom for notification purpose, now this jsp at the footer...

You have GOT to stop thinking in terms of JSP.
JSP just creates an HTML page - possibly with an applet or JavaScript but still HTML.
When I have done this sort of thing, I have started with plain HTML as a mockup of the final product. Solve one problem at a time. When you have a functioning plain HTML prototype, THEN figure out how to create a dynamic version with JSP.
I recommend the Firefox browser for developing/debugging JavaScript - the developer tools are excellent - I love being able to browse around the actual HTML DOM and change values.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending response to client with out receving request