aspose file tools*
The moose likes JSF and the fly likes JSF - Request / Session attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF - Request / Session attributes" Watch "JSF - Request / Session attributes" New topic
Author

JSF - Request / Session attributes

dwarakanathan thiru
Ranch Hand

Joined: Oct 14, 2009
Posts: 49
Hi,

I have a JBoss portal server and I have two applications (different EAR files) deployed in it.
I use JSF framework for both the applications.

I provide a link in my first application which takes the user to a page in the second application.
Now, how can I set and then pass parameters / attributes from the first application to second application?

I tried to get the backing bean of the first application, in the second application but I guess it wont work.
Every FacesContext is unique for each application. Isnt it?

And then I tried this, again I get only "null" as value:

To set the attribute in portlet session

FacesContext fcRequest = FacesContext.getCurrentInstance();
PortletSession objPortletSess = (PortletSession) fcRequest.getExternalContext().getSession(false);
objPortletSess.setAttribute("ATTRIBUTE_NAME", "VALUE", PortletSession.APPLICATION_SCOPE);


To get the attribute from portlet session

PortletSession objPortletSess = (PortletSession) fcRequest .getExternalContext().getSession(false);
String strSelFileId = (String) objPortletSess.getAttribute("ATTRIBUTE_NAME", PortletSession.APPLICATION_SCOPE);

Any help is much appreciated.

Thanks!!


Thanks,
Dwarak T
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15665
    
  15

No, every FacesContext is not unique to each application.

FacesContext is unique to each request. Not only is it not unique to an application, it's not unique to the user session. The FacesServlet builds it when a JSF request comes in, uses it to manage the JSF request/response process, then tosses it away once the reponse has been sent back. It builds a whole new FacesContext on the next JSF request that comes in. That's why generic servlets and JSPs get NullPointerExceptions. They don't build FacesContext's. Only the FacesServlet does.

There are several ways to share data between webapps and they're not JSF-specific. One way is to share session data. That tends to be fairly low overhead, but it only works in limited configurations. And you have to specially configure the server(s) that do the session sharing.

A more robust way is to simply keep the info in persistent storage. This is more flexible, but may be more overhead. The overall penalty can be reduced, though, if you use a shared cache mechanism.


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

Joined: Oct 14, 2009
Posts: 49
Okay, In that case, can you please tell me why request attributes are not passed over to the other JSP / JSF file?
It should be the same request right?

FacesContext is unique to each request. Not only is it not unique to an application, it's not unique to the user session. The FacesServlet builds it when a JSF request comes in, uses it to manage the JSF request/response process, then tosses it away once the reponse has been sent back. It builds a whole new FacesContext on the next JSF request that comes in. That's why generic servlets and JSPs get NullPointerExceptions. They don't build FacesContext's. Only the FacesServlet does.


I prefer to use the session / request to pass on the values from one portal application to another portal application.
Can you please tell me how to do that? I tried with PortletSession and it does not work.

There are several ways to share data between webapps and they're not JSF-specific. One way is to share session data. That tends to be fairly low overhead, but it only works in limited configurations. And you have to specially configure the server(s) that do the session sharing.
A more robust way is to simply keep the info in persistent storage. This is more flexible, but may be more overhead. The overall penalty can be reduced, though, if you use a shared cache mechanism.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15665
    
  15

If your first application presents a link to the second application, then clicking the link will go directly to the second application and the first application won't even factor into it. Once the first application has built the page with the link on it, you could drop a nuclear bomb on the first application's webserver for all the difference it would make. There's no mechanism passing the request from app1 to app2. So request parameters wouldn't factor.

The request parameters are coming from the URL (GET) or the form (POST) on the page (or page fragment, for portlets). Because the page is a text object it has no way to hold binary java object instances in it. Thus, the only way to pass binary objects from one server to another via HTTP is to pass a text "handle" that can be used by the backend of the target server to locate the actual binary object. However, if the binary object is residing in a different Java VM - or even in a different webapp classpath on the same VM, it won't be directly accessible by the receiving application.

That's why you usually need some sort of serializing mechanism to allow resources to be shared between webapps.
dwarakanathan thiru
Ranch Hand

Joined: Oct 14, 2009
Posts: 49
Finally I got a way that works.
I tried passing request parameters from the 1st application to the 2nd application using a servlet in the 2nd app.
And from there I will set the parameter back to a session & use a redirect to my portal page, which will use up the parameter.

I shall post the code soon.
Bab Sm
Greenhorn

Joined: Jun 13, 2012
Posts: 1
dwarakanathan thiru wrote:Finally I got a way that works.
I tried passing request parameters from the 1st application to the 2nd application using a servlet in the 2nd app.
And from there I will set the parameter back to a session & use a redirect to my portal page, which will use up the parameter.

I shall post the code soon.


Thiru, could you please post the code. I'm trying to do exactly the samething.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15665
    
  15

It's a good idea to check the date when responding to an old message. This thread was last posted to about 2-1/2 years ago. These days, that means that some or all of the participants may not even be working for the same employer any more, much less working on the same project.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF - Request / Session attributes
 
Similar Threads
How to manage sessions in servlet ?
Portlet Communication: What is application scope, anyway?
Parameter passing from one app (EAR) to another EAR
Show details of story on a different page of portlet
getting PortletSession in JSF