wood burning stoves*
The moose likes JSF and the fly likes How to call JSF backing bean method from the Applet    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "How to call JSF backing bean method from the Applet    " Watch "How to call JSF backing bean method from the Applet    " New topic
Author

How to call JSF backing bean method from the Applet

sathishgopal gopal
Greenhorn

Joined: Mar 05, 2005
Posts: 4
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?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Where exactly do you need the FacesContext for?
sathishgopal gopal
Greenhorn

Joined: Mar 05, 2005
Posts: 4
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.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16095
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Mar 05, 2005
Posts: 4
Yes i need it because my backing bean gets some Value from the HttpSession like..
FacesContext.getCurrentInstance()
.getExternalContext().getSessionMap();
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
That being the case, why can't you use a servlet? It provides access to the HttpSession as well.


Ping & DNS - my free Android networking tools app
sathishgopal gopal
Greenhorn

Joined: Mar 05, 2005
Posts: 4
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
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
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

Joined: Jun 25, 2001
Posts: 16095
    
  21

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

Joined: Oct 08, 2006
Posts: 2458
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 ]
bhaskar mellamputi
Greenhorn

Joined: Jan 13, 2009
Posts: 1
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.
Eirik Larsen
Greenhorn

Joined: Feb 13, 2009
Posts: 1
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
Pablo Barba
Greenhorn

Joined: Mar 26, 2009
Posts: 1
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

Joined: Oct 08, 2006
Posts: 2458
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.
 
GeeCON Prague 2014
 
subject: How to call JSF backing bean method from the Applet