• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we Access FacesContext outside FacesServlet

 
Mahendra Pratap
Ranch Hand
Posts: 42
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am facing a problem related to facescontext, I need to access the facesContext in an external servlet, is there some way to achieve this? After some precessing on that Servlet i am navigating to jsf page, and i want jsf page to access the same facesContext.

Thanks,
Mahendra
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FacesContext does not exist except when JSF is actively processing a JSF request. Sorry.
 
Mahendra Pratap
Ranch Hand
Posts: 42
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we make a servlet react in a way like it is serving an active JSF request.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahendra Pratap wrote:Can we make a servlet react in a way like it is serving an active JSF request.


There is one. It's called FacesServlet. But it doesn't act like it, it actually does it. In fact, it's what creates and destroys the FacesContext for a request.

The real question is: what do you need to do?
 
Mahendra Pratap
Ranch Hand
Posts: 42
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:
There is one. It's called FacesServlet. But it doesn't act like it, it actually does it. In fact, it's what creates and destroys the FacesContext for a request.

The real question is: what do you need to do?


I am navigating from application to a pdf page and then from that pdf page on click of a button i need to do some processing and save a facesmessage and then navigate back to application. Everything is done but i am getting nullpointer when i am going to access facesContext to set the message. Any input/Help will be appriciated.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A PDF isn't a "page". It's a document that has to be downloaded and displayed by an independent application such as Adobe Reader.

You can put hyperlinks inside PDF, but what they actually do is just allow the reader program to invoke a web browser and pass it the URL as an HTTP GET request. JSF works mostly off HTTP POST, so that would have been a problem anyway. Plus, like I said. FacesContext is created and destroyed on each and every JSF view request and only on JSF view requests. It has no long-term existence at all. When a JSF page accesses the FacesContext to add a message, that message is going to be used by the outgoing (response) part of the JSF page processing (usually to render the h:message or h:messages tags). Once the response has been sent, the FacesContext and messages objects are destroyed.

When (if) a new JSF page request is received, the FacesServlet will create a new FacesContext to handle that page. This new FacesContext won't contain any messages until either you or the JSF infrastructure add messages.

In other words, a PDF can't create a FacesMessage directly. The best you can do is create a URL that will create that message when the hyperlink in the PDF is clicked.
 
Mahendra Pratap
Ranch Hand
Posts: 42
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes we have to provide a link on pdf.. and that link is of a servlet and from that servlet it will navigate to application. I am able to get an instance of facescontext now by creating a new instance in servlet as below

 
Tim Holloway
Saloon Keeper
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You would probably be much safer to simply have the link point to a JSF URL. Creating your own JSF FacesContext without the additional infrastructure of the FacesServlet is extremely hazardous. Even if you don't run across any "gotchas" today, a JSF version updare could cause problems tomorrow.

There's a product called "PrettyFaces" (ocpsoft.com) that's good for stuff like this - it allows you to create bookmarkable JSF URLs with parameters and to use them without having to create a lot of specialized code. It's fairly easy to install and configure and the author has been known to appear in this forum occasionally.
 
Mahendra Pratap
Ranch Hand
Posts: 42
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can achieve the same by pointing a link directly to JSF url.. but as soon as the JSF page will load i need to redirect it to some other jsf page.. is it possible to redirect from one jsf page to some other page without any jsf action. Might be using some event like page load complete or some other way? actually i can use

but the problem is now all the session values get cleared.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a suspicion that you've got business logic in a backing bean and maybe you should move out of the backing bean and into a business bean so that it can be shared by backing beans for the 2 pages instead of trying to redirect.

It wouldn't be that uncommon to have a request come in on one JSF URL and have the request process, then navigate to a different View, but that's not the same as redirect. Redirect is normally done instead of processing, not in addition to processing.

Part of my confusion is what you really mean by "page load".

In a URL GET JSF request, the request comes in, the View is set up and the page loads into the user's browser. At that point, the HTTP request/response cycle is at an end. You cannot redirect - you have to initiate another request/response, either by the user taking manual action or by client-side automation (JavaScript or META tag). This is what I interpret when someone says "page load". I think you mean something different, though.
 
H Jetly
Ranch Hand
Posts: 41
Google Web Toolkit Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahendra wrote:
I can achieve the same by pointing a link directly to JSF url.. but as soon as the JSF page will load i need to redirect it to some other jsf page.. is it possible to redirect from one jsf page to some other page without any jsf action. Might be using some event like page load complete or some other way? actually i can use


My question is, if the first JSF page will only load and then you have to redirect to another JSF page, why cant you perform all the activities in the second JSF page?
Can you please give a detail of what you are trying to achieve by this redirect.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic