• 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

How to call JSF backing bean method from the Applet

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I'm writing a small JSF application which uses Applet for certain task. I need to call an existing JSF backing bean method from the applet. Let me elaborate my requirement.

A JSF backing bean(*MyBean* - kept in Session) redirects to another JSF page. This JSF page loads an applet. This applet displays some records. Until this it works fine.

User triggers event on the loaded Applet (Pagination for the loaded records). I would want to do some Server side processing(Call DB and get new data and loaded the same applet again). The actual logic of calling the POJO/EJB Service is part of the backing bean(MyBean. It does this for the first time).

Now, how do i call the backing bean method.

One solution that i found is.

The event triggered by the user on the applet will send URL connection to the servlet. I can get reference to the backing bean from Session scope and invoke the appropriate method. The issue with this approach is that it does not follow JSF lifecycle. Hence FacesContext will always null..

Is there any other way to do this?
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where exactly do you need the FacesContext for?
 
sathishgopal gopal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need Faces Context object in my Servlet. The actual job of creating the FacesContext is done by the FacesServlet.

My Applet opens a connection to talk to the Servlet. This Servlet fetchs the backing bean object from the HttpSession. Then i invoke a method in the reterived Backing bean object. I need to bind FacesContext object for this request. I copied the code from FacesServlet (for creating FacesContext via FacesContextFactory) to my Servlet, it works. There must be some other way to do this.
 
Saloon Keeper
Posts: 27763
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
I'd be a little nervous about that. The whole point of JSF is that it provides a conversation context with POJO's. If you don't need the back-and-forth and the renderers, you shouldn't normally need JSF. Just front the POJO with a servlet and do AJAX-style requests from the applet through the fronting servlet and into the backing bean. By "AJAX-style", I mean any "asynchronous" communications that access selected parts of the bean, not just XML ones.

It's perfectly OK to share a JSF backing bean between JSF and non-JSF access.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sathishgopal gopal:
I need Faces Context object in my Servlet. The actual job of creating the FacesContext is done by the FacesServlet.

My Applet opens a connection to talk to the Servlet. This Servlet fetchs the backing bean object from the HttpSession. Then i invoke a method in the reterived Backing bean object. I need to bind FacesContext object for this request. I copied the code from FacesServlet (for creating FacesContext via FacesContextFactory) to my Servlet, it works. There must be some other way to do this.


Please be more specific. Do you need it, because you want to access ExternalContext#getSessionMap() or so?
 
sathishgopal gopal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i need it because my backing bean gets some Value from the HttpSession like..
FacesContext.getCurrentInstance()
.getExternalContext().getSessionMap();
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That being the case, why can't you use a servlet? It provides access to the HttpSession as well.
 
sathishgopal gopal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what i have done. Applet will be calling this Servlet. The Servlet looks up the backing bean from the HTTPSession. Then the Servlet call the backing method (which in turn Query the DB via DOA).

unfortunately the backing bean method i'm calling is written by someone else and has dependency with FacesContext object(it can't be null).I copied code from FacesServlet to MYServlet to create FacesContext Object and bind it for this request(Thread). It works fine but only thing is, What FacesServlet should do is being duplicated in another Servlet.

Is there any better way to do this.
 
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
The obvious way to solve this is to make the backing bean independent of the view technology. That's how you design backing beans to begin with.
 
Tim Holloway
Saloon Keeper
Posts: 27763
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

Originally posted by Ulf Dittmer:
The obvious way to solve this is to make the backing bean independent of the view technology. That's how you design backing beans to begin with.



Amen. I swear the worst thing about JSF is that it's so simple that people feel obliged to create complex solutions when they don't need to.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sathishgopal gopal:
Yes i need it because my backing bean gets some Value from the HttpSession like..
FacesContext.getCurrentInstance()
.getExternalContext().getSessionMap();



Do you know that the ExternalContext#getSessionMap() returns a mapping of HttpSession#getAttribute() and setAttribute()? So that whenever JSF puts something in the SessionMap, that exactly the same attribute is just accessible by HttpSession#getAttribute() inside a servlet in the same session and vice versa? That same applies to session scoped managed beans which are accessible as session attributes by their managed bean name.
[ August 26, 2008: Message edited by: Bauke Scholtz ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sathisgopal,

I am new to Java coding and needs to load applet into jsf applicaiton. I have problem loading applet into jsf application. I build jsf in jdev and runs on oc4j server. I implemented applet class and tried to load into the jspx page. It thows error no class found. But when i tried same client code in html with applet tag it works fine and applet is loaded.

I saw in your post you are able to load applet into JSF and not able to communicate with backing bean.

Could you please provide information anything that i missing

Thanks,
Bhaskar.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sathishgopal gopal,

From your requirements described above I understand that you need to make a faces request to the server. Here's a trick that makes you able to do that.

- In your applet, use JSObject (included in plugin.jar in your jre) to call a javascript method on your page.
- In this javascript method, fill in some jsf hidden fields with whatever you want to post to the server. Do a javascript submit in your jsf form.
- When the server completes the jsf requests, use a javascript-hook in some component on the page that has a "oncomplete" attribute. In the oncomplete attribute call a another javascript function.
- This function takes the value from a jsf-component that was just updated from the server and calls a method on your applet with the jsf-components value as a parameter.

If you're unsure how to communicate from applet to javascript and vice versa check this tutorial:
http://www.raditha.com/java/mayscript.php

Happy coding

-Eirik
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I would like to send an scanned image from an applet to a backend bean to process it.
But the problem is that I don't know how applet could communicate with the backend bean.
If it was a communication with a normal servlet there wasn't any problem because applet can send it with a http client method.
But in JSF the communication is more complex due it isn't so transparent.
Any ideas?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're resurrecting an old thread with the same question and with the answers already given. Please do not focus the question only, but also read the answers.
 
reply
    Bookmark Topic Watch Topic
  • New Topic