• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I want observer/observable implemented in Servlet/Applet

 
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Uff,

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

Mak Jav
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic