8

I feel like this is going to be a question to which the shortest answer will be : "that's why JSF replaced JSP", but I'll just go ahead and ask it.

Question : I am wondering : could I obtain the Response object of a JSF page (if there's any) ?

Why wonder ? : I found myself in a situation where I need to to pass from a JSF page to a JSP one, so I thought why not redirect (with response.sendRedirect) from a bean that gets invoked from the JSF page and then... you can see where it's heading.

I feel like this can be done in a cleaner way, can't see how though !

EDIT : while on it, I'll also ask about which way would be best for redirecting from to JSF pages.

Thanks in advance for your suggestions.

Akheloes
  • 1,352
  • 3
  • 11
  • 28
  • Not sure to understand the real problem, but I can suggest to use `` as shown [here](http://www.mkyong.com/jsf2/jsf-2-link-commandlink-and-outputlink-example/). – Luiggi Mendoza May 27 '13 at 16:48
  • Actually I meant the question to be of a general order, so anything about redirecting in JSF will do me good :) So outputLink replaces the `` tag, now what about redirecting from inside a bean ? – Akheloes May 27 '13 at 16:56
  • You can obtain the response object in the managed bean by calling `HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse()` (obtained from [How to stream a file download in a JSF backing bean?](http://stackoverflow.com/a/9394237/1065197)) and then perform operations on the `HttpServletResponse` object. Obviously, there are things that you can't do like sending a redirect from an ajax request. – Luiggi Mendoza May 27 '13 at 17:04
  • Ok. This is weird, same answer from two guys in practically the same time... If you can post it as an answer it so I could at least be able to upvote it. – Akheloes May 27 '13 at 17:07

2 Answers2

10

Well, if you want to get the response object, you can have it in JSF like bellow!

HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

But you don't really need to get the response object only to redirect outside of JSF. This can be done more easily with the following:

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.redirect("http://www.example.com/myJspPage.jsp");

Edit:

When you are in any non-action method, you can use any of the above! But when you are in any action method, the proper JSF way of redirecting is:

public String goToOutsideAction(){
    ....
    return "/myPage.xhtml?faces-redirect=true"
}

The method should return a context-relative view ID and the target must be a JSF page.

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
  • Thanks, `page.xhtml?faces-redirect=true` works as well, which one is best ? – Akheloes May 27 '13 at 17:18
  • Yup! Ofcourse, `return "url.com/page.jsp?faces-redirect=true"` is the proper jsf way to handle redirect, for details you can see the answer of [this](http://stackoverflow.com/questions/11277366/what-is-the-difference-between-redirect-and-navigation-forward-and-when-to-use-w) question. – Sazzadur Rahaman May 27 '13 at 17:35
  • Mind me asking though, could redirecting to a JSP page from a JSF one be cause for trouble ? – Akheloes May 27 '13 at 19:24
  • 2
    @Gloserio nope, it will just generate a GET request on the JSP page. – Luiggi Mendoza May 27 '13 at 22:47
  • @LuiggiMendoza : so what about the beans that were in the JSF page when you redirect to a JSP, do they suffer nothing in particular ? How do they behave ? – Akheloes May 28 '13 at 08:41
  • The code in your edit is completely wrong. The method can't be `void` and the outcome can't be a full URL. It must be a context-relative view ID and the target must be a JSF page, not a "plain JSP" page as the code seems to suggest. JSP is deprecated since JSF 2.0, so it's really strange to see a JSF 2.0 targeted piece of code which still uses JSP. – BalusC May 28 '13 at 11:33
  • @BalusC : so should not navigate from JSF page to JSP page ? – Akheloes May 28 '13 at 11:43
  • Replace "should" by "can". Note that this is exactly the whole reason why the OP posted this question. – BalusC May 28 '13 at 11:46
  • Can not ? Why not ? What's the hasard ? – Akheloes May 28 '13 at 11:54
  • @BalusC, Yes. I see, you are right (Some how I missed it to get earlier). And the `void` was a typing mistake.. :P Anyways thank you very much for your suggestions. And I just edited my answer. – Sazzadur Rahaman May 28 '13 at 13:11
1

You can obtain the response object in the managed bean by calling

HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse()

Code adapted from How to stream a file download in a JSF backing bean?)

Once you have the response object you can perform any operations on it like changing the headers. Obviously, there are things that you can't do like sending a redirect from an ajax request.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Thanks, would it be ok with you if I marked @Sarradur Rahaman 's answer as the accepted one ? It seems he was the first... – Akheloes May 27 '13 at 17:16