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

Servlet to applet communication

Jemal Mohammed
Greenhorn

Joined: Jul 05, 2007
Posts: 9
hello guys

I am developing an applet which displays the current weather. The applet gets the data from a database through a servlet.
The servlet sends a data to the applet only when a request is invoked by the applet.
Is there a way that the servlet sends data to the applet whenever there is a change in weather without the applet sending request.

thank you
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41906
    
  63
Welcome to JavaRanch.

It's not possible in general for a server to make contact with an applet. But it's not much overhead for the applet to send a short request every few minutes or so to check if something has changed (or maybe even longer timeframes, depending on how often the server data is updated).

But is an applet really the right tool for a job like this? It would rely on a web page being continuously open.


Ping & DNS - my free Android networking tools app
Jemal Mohammed
Greenhorn

Joined: Jul 05, 2007
Posts: 9
Thanks Ulf Dittmer for the fast response

I was thinking a continuous request would have a great impact on the performance. I will try it.

I am choosing an applet for this application becuase 1)it is web based
2)I need the applet to plot a waveform(chart) accordingly.

Is there any other way to do the plotting on a web browser.

Thank you
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Jemal Mohammed:

I am choosing an applet for this application becuase 1)it is web based
2)I need the applet to plot a waveform(chart) accordingly.

Pooling for new data from time to time would be good for you application.

You can even try AJAX for this kind of situations.From javascript only you can make HTTP requests to the server asynchronously and then you get the response back.Using that response data ,you can again use javascript for making graphs.

There might be many ready to use AJAX components might be available for this purpose.


Rahul Bhattacharjee
LinkedIn - Blog
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41906
    
  63
Originally posted by Rahul Bhattacharjee:
you can again use javascript for making graphs.

Can you point us to an example of this? I always thought of graphing as being outside of the realm of HTML/CSS (and thus JavaScript). I'd be very interested in seeing that in action.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Rahul Bhattacharjee:


You can even try AJAX for this kind of situations.From javascript only you can make HTTP requests to the server asynchronously and then you get the response back.Using that response data ,you can again use javascript for making graphs.



If he's already got an applet on the page, there is no need for AJAX.
Applets have been able to make HTTP calls to the server for a lot longer that Javascript can. Furthermore, with an applet, you can eliminate the need to build and parse JSON, XML, etc.. by streaming serialized Java objects back and forth.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This would be outside the realm of servlets but, if your traffic isn't too high you might want to consider keeping a socket open between the applet and your server.

This is how AIM, and YIM work.
Another example would be the atomic clock at http://www.time.gov.

One downside to this is that it requires another port to be open.
This can be a hassle for a lot of companies with strict firewall policies.

If I were doing this, I would probably follow Ulf's advice and have the applet poll the server every n seconds.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
I found the following link ,

URL-1 , This might not be using ajax , but if we can make out a graph in the client side then we should be able to use AJAX to fetch changing data from server time to time.

There are many third parties (http://www.activewidgets.com/) which sell ready to use AJAX based GUI components.There might be something like this for making graphs.
Well I have never used graphs , but used data grids though.

Ben Souther: If he's already got an applet on the page, there is no need for AJAX.

Thats true.I just wanted to let him know that there might exists ready to use AJAX based component for his graph requirement.

[ July 06, 2007: Message edited by: Rahul Bhattacharjee ]
Jemal Mohammed
Greenhorn

Joined: Jul 05, 2007
Posts: 9
thank you all for your suggestions.
Jemal Mohammed
Greenhorn

Joined: Jul 05, 2007
Posts: 9
hello guys I am done with the database access. Now I am now developing a chat using applets and servlets. I am having a difficulty in sending the a user name to to the applet.
the first html page accepts user name and this will be sent to a servlet which will then redirect to the applet page. now my problem is in sending the user name to the applet. Note that my applet's get and post methods are used for requesting new message and sending a message.

Thank you
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41906
    
  63
Can you create the applet tag dynamically, and add a "<param name="username" value="...">" to it?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

For the OP Jemal Mohammed,

Why dont you try Google Maps? I heard that its useful for plotting Graphs into the Web Applications. (if its not too late).


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Jemal Mohammed
Greenhorn

Joined: Jul 05, 2007
Posts: 9
that is my problem. How can I make the 'value' field in the
<param name="username" value="..."> a varaible so that the value of this variable be changed dynamically

any help
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41906
    
  63
Well, you have the username in the servlet, correct? If you make the page containing the applet a JSP page, you can pass the username from the servlet to the JSP page as a request attribute.
Jemal Mohammed
Greenhorn

Joined: Jul 05, 2007
Posts: 9
thank you Ulf Dittmer it works!!

Now i have another problem in the chat application.
How can i get the names and number of users online who are chatting.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41906
    
  63
Let's not mix different topics in the same thread. I see you have created another thread for that question already, so any discussion should happen there.
 
Don't get me started about those stupid light bulbs.
 
subject: Servlet to applet communication