aspose file tools*
The moose likes Servlets and the fly likes I want observer/observable implemented in Servlet/Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "I want observer/observable implemented in Servlet/Applet" Watch "I want observer/observable implemented in Servlet/Applet" New topic
Author

I want observer/observable implemented in Servlet/Applet

Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
I want to have an applet that is automatically updated from a Servlet over
http. This will be a Obsever/Observable pattern along with a Server push
type of thing. Please help me in this regard...

Thanx in Advance,

Maki Jav


Help gets you when you need it!
David Mace
Ranch Hand

Joined: May 26, 2004
Posts: 35
In order to do a server push, I think that you're going to have to open a socket on the applet which will let the server initiate a conversation with the remote applet. You could register your observers by having them send an initial call to the servlet once they have been init'd to have them be registered as observers.

Now, there are some problems here: what port are you going to use for receiving communication to the applets and is the client computer behind a firewall? If you're deploying this in a coorporate environment where you have control over these things, it may not be as much of an issue, but still the idea sounds a little error-prone to me.

You can push your data in whatever format you would like, perhaps xml or as a binary serialized object and then have your applet handle this data however you would like.

That's just my thoughts 'off the cuff' which you may or may not find useful.

Thanks,
David


Dad always thought laughter was the best medicine, which I guess is why several of us died of tuberculosis. -Jack Handy
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
I think of doing a http tunneling sort of thing.... along with server push.

What options do I have? No, I don't want to use AJAX...

I would appreciate some code...

Thank you,

Maki Jav
[ February 24, 2006: Message edited by: Maki Jav ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

HTTP Tunnelling is pull technology.

In order to implement a server push, you'd need to make some kind of a stateful connection on another port.
If you run some Google searches for "Java Chat Server" or "Java Socket Programming" you should find some code examples.


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

Joined: May 09, 2002
Posts: 437
Ah yes Ben, it is a pull technology, I just mixed up some concepts. Sorry for that.

One thing I can do is as mentioned by David is to register my applets with the servlet. I can use the ip addresses for that. But what will I have to do to avoid using a continously running thread that will check whether any value has arrived from the servlet. I want something similar to what we do with local classes. The value that is get send from one class is displayed automatically in a textfield of the other for example.

Maki Jav
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

But what will I have to do to avoid using a continously running thread that will check whether any value has arrived from the servlet.


I had done a similar kind of work(servlet/applet along with xml) with sockets and threads. But I had followed exactly this kind procedure in my application. Using observer design pattern to notify the model class about new inputs and using threads and sockets send the output to applet.

The problem here is that a class will always be running in a thread listening in a particular port which will be accessed by applet.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
If this was my problem I would just have the Applet poll the servlet every X milliseconds. Soooooo much simpler than any scheme that has dedicated ports and it will be transparent to firewalls. Furthermore there will be less new protocols to invent.

Aha - I hear you thinking - that will be slow and inefficient - balderdash. A simple GET or even HEAD request can be processed in a few milliseconds and will be enough to let the applet know whether or not to request a full update. You could even dynamically adjust the X milliseconds figure according to recent history, user interaction, etc.

See the Occam's Razor entry in Wikipedia

Yours for simplicity, Bill
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Aha! you heard me thinking, great! but the content? you got it all wrong! I thought "My question has been so good that it pulled William Brogden's response!"

Occam's Razor I will follow honestly. Now I am not thin skinned or just don't fashionably grumble "oh God! how heavy[slow] it will be!". I have used Java and know it's strengths too!

William Brogden, I highly respect your opinion, as I know that you are no ordinary man and that you should not be taken lightly.

So be it, I will follow what William has suggested. It means that we will have to drop the idea of Observer/Observable and follow the simple applet/Servlet communication design or a bit more complex of applet getting an object ie a vector containing new values if the data has changed.

So we has now two servlets. One say an ObserverServlet and the second ValueUpdaterServlet. Any user entering values will be using the second servlet. ObserverServlet is updated for new values too. Applets retrieve these values by polling.

Questions:

1) How to check the headers for any update? Should I send back a simple string "yes" or "no" as indicator of whether to proceed further and readObject or not?

2) I read somewhere that threads n applets are heavy? I don't agree, what is your opinion? and who are these guys? are they giving us sophisticated ideas or are they trying to get people frightened and make them run away?

Maki Jav
[ February 25, 2006: Message edited by: Maki Jav ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
1) How to check the headers for any update? Should I send back a simple string "yes" or "no" as indicator of whether to proceed further and readObject or not?

Why not have the response header Last-Modified date tell the applet the timestamp on the most recent data, then the applet can decide if this matches what it has in memory. No need for any content, just the headers will suffice. The ObserverServlet and ValueUpdaterServlet can share a cache of results.
Threads in Applets have to share CPU time with other stuff the browser is doing so if your page is running some complex animations or retrieving a bunch of images, sure it will be slower. However, compared to all the other delays in the network, that would be the least of my worries.
As to where some of these bizarre ideas about Java speed come from, I have no idea!
Bill
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi again,

I will only be running a single thread to poll the servlet every 3 seconds ,so I hope it will not be a problem. My immediate requirement is for an intranet.
Earlier I was thinking of using Datagram Packets for communication but then again whether TCP/IP Sockets or connectionless packets/sockets, there has to be a thread running on the client side that will check for incoming data. Now as there is no escape from the while loop on client side, the best solution was suggested by William Brogden.

May I ask which jre should I use in my given situation. I used jdk1.3 for applets in my November 2005 project. It loads awt applets quickly.

About those guys I can say only this that they are too nervous to have fun.
Fun is doing what you enjoy....
Which is Java in my case


Maki Jav

ps: William Brogden I have that book of yours!
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi again,

Problem again...

I have written two servlets
1) ObserverServlet
2) SaverServlet

And a model class which holds new value, ChangeHolder.java. It is an Observable
class.

SaveServlet.java is used to update value from users. This update ChangeHolder
field value.

ObserverServlet.java observes value changes in ChangeHolder class and updates
its String value and long date fields. Applets approach this class for
information...

The problem is my applet gets held when using appletviewer or IE and not when
I use opera 8.

This is the code in my ObserverServlet in Get method



and the corresponding code in my applet is:



I can send you the four uncompiled classes if required,

Thanx in Advance,

Maki Jav

[ March 01, 2006: Message edited by: Maki Jav ]
[ March 01, 2006: Message edited by: Maki Jav ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
I can't think why the applet would work in one browser but not in the applet viewer. I suspect the request is not being built correctly.

If this was my problem I would not be assembling the headers that way. Since your URL starts with "http://" openConnection gets you a HttpURLConnection. That class has the methods needed to set the request method - which should be POST, not GET, if you want to read an ObjectInputStream. So your servlet should handle things in the doPost method.
Use the setRequestProperty method to set all those header fields instead of trying to build the String - HttpURLConnection is probably sending the headers it wants to send, followed by a blank line followed by the String you build as "headers". Very confusing to the doGet method which should only look at headers, not a body.
Bill
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Chiming in late ... my work system has an applet with an open server socket. The server can push messages to the applet which passes them on to JavaScript or other POJOs. One challenge is registering all the applets with a cluster of servers so any server can push to any client.

This is part of a product that pushes messages that require very low latency, eg telephony events. Polling every few seconds would not meet requirements and a couple thousand users polling 15-20 times a minute adds up to a lot of hits.

We have one use case that pushes a business alert (eg don't sell auto insurance in front of that hurricane) to many users. In this case having every client poll once a minute could be much faster than sequentially connecting to each applet, pushing a message and disconnecting.


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
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi,

I have problem again. The dates of applet and servlet do not match even when servlet updates the applet.....

My applet code:




And my observer servlet is:



And My observable class is:




Please help me with this,

Thanx in Advance,

Maki Jav
[ March 03, 2006: Message edited by: Maki Jav ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
You seem to use both date objects you create, and date objects the HttpURLConnection class creates, and it's not clear to me which ones of those should match and why. Maybe you can give a brief description of what you're trying to achieve.


Ping & DNS - my free Android networking tools app
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Ulf,

Simply put, the applet will poll the servlet for newer information. Applet will have a date value for the information it last got ie how old this info is? Similarly, the servlet will have the date value for the last time its information was updated (by ChangeHolder class).

whenever the applet sends request for that data (information), it sends date first. The servlet decides whether to send back data or refuse by comparing the two dates.

Hope I made my point clear,

Thanx in advance,

Maki Jav
[ March 04, 2006: Message edited by: Maki Jav ]
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Sorry no time to read so many posts, my appologies if duplicated. I remember in 1998 I used an approach without polling in a chat applet/servlet communication. Applet just connected to servlet and listened command and send responses. If connection became bad for some reason, applet just reconnected. I think I used HTTP for deceive firewalls. I am not sure that it's scaled well however we had about 200 applets connected at the same time without big CPU and memory impact for a servlet container.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
There should only be one source for dates, and that should be the server. If the server sends data, it should send a date along with it. The applet should remember that date, and send it back to the server when it asks for new data.
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
IF you see my above post and code, this is what is exactly happening. The servlet updates the applet. but the problem is somewhere else. If you copy and run my code you will see that servlet is send a date and applet is getting it storing it, but the dates do not match...

Thanx in advance,

Maki Jav
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
I'm not sure that's what's happening - the fact that it doesn't work suggests it isn't. I've never used "date = connection.getDate();" - are you sure it returns the date header of the response, and not the one of the request?

Instead of hijacking a header that has a predefined meaning, I would use one with a distinctive name, in order to avoid name conflicts.
[ March 05, 2006: Message edited by: Ulf Dittmer ]
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Yes Uff,

I will just send plain header with a unique name along with date value ...

Mak Jav
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: I want observer/observable implemented in Servlet/Applet