• 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

Asynchronous javascript calls

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bronto Brontkevich
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. I understand now why FacesContext is not available sometimes. But I still not understand how to execute javascript asynchronously!
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic