wood burning stoves*
The moose likes JSF and the fly likes Asynchronous javascript calls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Asynchronous javascript calls" Watch "Asynchronous javascript calls" New topic
Author

Asynchronous javascript calls

Bronto Brontkevich
Greenhorn

Joined: Dec 10, 2010
Posts: 10
I have an application on icefaces 1.8.2
I need to execute some javascript asynchronously. I.e. I got some server event (from my server lifecycle but not on some stage of jsf lifecycle) and I need to call javascript on clients side reporting about this server event. I use such code for this:


But sometimes it is being not executed. By the way. FacesContext is taken from special field in Managed Bean (I store not null value of FacesContext manually). Because FacesContext.getCurrentInstance() sometimes gives me null. I killed several virgins and God answered me that: "FacesContext.getCurrentInstance() returns null if you are not within a request that is handled by the FacesServlet". I don't know another way to get FacesContext in my case. So I just keep it in field of my managed bean. May be I loose javascript calls because of this hack?
I have some other versions why my code does not guarantee asynchronous javascript execution. But may be somebody knows exactly what is a problem and how to do what I want?
Bronto Brontkevich
Greenhorn

Joined: Dec 10, 2010
Posts: 10
I've changed my code so that I always get not null FacesContext. But still loosing some javascript calls.
Next version.. It is possible then after onDemandRenderer.requestRender(); will complete next one onDemandRenderer.requestRender(); will be initiated. Will second requestRender call annul javascript call that was done before first requestRender call?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

You cannot store FacesContext. FacesContext is created once per HTTP request and destroyed when the response to that request is returned. And it is only created for JSF URL requests, not JSPs or servlet requests which is why attempting to get a FacesContext in these environments returns null. Saving a FacesContext will simply provide you with an invalid FacesContext, since the attached request will be over and done with by the time the next request comes in.


Customer surveys are for companies who didn't pay proper attention to begin with.
Bronto Brontkevich
Greenhorn

Joined: Dec 10, 2010
Posts: 10
Yes. I understand now why FacesContext is not available sometimes. But I still not understand how to execute javascript asynchronously!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

Asynchronous JavaScript is the "AJ" part of AJAX.

You can do brute-force AJAX on a JSF page if you really want to, and there are plenty of websites that tell how to do so. Even some support libraries such as dojo, and jQuery. Or at least, I think jQuery can do AJAX. I could be wrong on that one.

However, a lot of work has been done to make JSF more AJAX-friendly. A number of the third-party extension tagsets such as RichFaces and IceFaces provide such good AJAX support that you often have to write little or no JavaScript at all!

JSF 2.0 also adds AJAX support to the core capabilities of JSF itself.
Bronto Brontkevich
Greenhorn

Joined: Dec 10, 2010
Posts: 10
Thank you very much for reply!
But your answer is too abstract. Was my question as abstract or muzzy?
Of course I know and can a lot about client -> server interaction. Always when server handle some event from client there is no problem to use JavascriptContext.addJavascriptCall(facesContext, script); And in this cases FacesContext.getCurrentInstance() always gives not null object.

But my question was how to initiate javascript in server -> client interaction! I would like to get something certain like "JavascriptContext.addJavascriptCall(facesContext, script);" And how can jQuery or dojo help me to send javascript from server? I did not work with them. I have icefaces 1.8.2 now. But I think jQuery and Dojo used on client side?
Yes. I learn new features of icefaces 2.0 now. And your answer invigorates my learning. Thanks!

Nevertheless I still believe in more certain answer for my question...
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

If you're asking what I think you're asking, it isn't possible.

Unlike client/server apps, HTTP apps are one-way. Clients can solicit the server, but the server cannot solicit clients, only respond to client solicitations. It's not a JSF issue or even a Java issue. You cannot make an HTTP server of any kind reach out and invoke JavaScript on a web page.

That, in fact, is why AJAX was invented. While the server can't contact the client, an AJAX client CAN contact the server. It simply makes an HTTP request from within the page that contains it. To make the AJAX code run, you usually either tie it to some GUI control or you fire off a client-side timer event.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Asynchronous javascript calls