JSF : Page Forward vs Page Redirect

By default, JSF will performs a server page forward while navigating to another page. See following example to differentiate between page forward and page redirect.

A “start.xhtml” page, with a button navigate to “page1.xhtml” page.

1. Page Forward

Here’s how the page forward works :

  1. Browser send a “GET” request to URL : http://localhost:8080/JavaServerFaces/faces/start.xhtml.
  2. JSF received the request and return the “start.xhtml“.
  3. Browser display the content of “start.xhtml“.
  4. User click on the button.
  5. JSF received the action and perform an internal page forward to “page1.xhtml” in the server side.
  6. JSF return the “page1.xhtml“.
  7. Browser display the content of the “page1.xhtml“.

In the page forward, browser’s URL is not update.

jsf2-page-forward-example

2. Page Redirection

Here’s how the page redirection works :

  1. Browser send a “GET” request to URL : http://localhost:8080/JavaServerFaces/faces/start.xhtml.
  2. JSF received the request and return the “start.xhtml“.
  3. Browser display the content of “start.xhtml“.
  4. User click on the button.
  5. JSF received the action and send back a “redirect” to “page1.xhtml” response back to the browser.
  6. Browser received the response and send another “GET” request to URL : http://localhost:8080/JavaServerFaces/faces/page1.xhtml.
  7. JSF received the request and return the “page1.xhtml“.
  8. Browser display the content of the “page1.xhtml“, and the browser’s URL is updated.
jsf2-page-redirection-example

To enable the page redirection in JSF 2.0, you can append “faces-redirect=true” at the end of the outcome string.

Page forward.


<h:form>
    <h:commandButton action="page1" value="Page1" />
</h:form>

Page redirection.


<h:form>
    <h:commandButton action="page1?faces-redirect=true" value="Page1" />
</h:form>

In the navigation rule, you can enable the page redirection by adding a <redirect /> element within the <navigation-case />.


<navigation-rule>
	<from-view-id>start.xhtml</from-view-id>
	<navigation-case>
		<from-outcome>page1</from-outcome>
		<to-view-id>page1.xhtml</to-view-id>
		<redirect />
	</navigation-case>
</navigation-rule>

Conclusion

Default page forward mechanism is more faster if compare to page redirection, because the page redirect added extra HTTP request to the server. So, only enable the page redirect when necessary, for example, uses Post/Redirect/Get Design Pattern to solve the classic duplicated form submission problem.

Download Source Code

Download it – JSF-2-Page-Redirection-Example.zip (9KB)

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
25 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
25 Comment authors
meCarolSachin SinghDambyAlvaro Urbaez Recent comment authors
newest oldest most voted
Toan ND
Guest
Toan ND

Can use “Page Redirection” with “POST” action and no using navigation config?
Thanks

Neo
Guest
Neo

This is exactly what I need!

me
Guest
me

nice

Carol
Guest
Carol

Thanks for sharing

Sachin Singh
Guest
Sachin Singh

I have a doubt that u have mentioned “GET” in the above example bt i have read that JSF by default uses “POST”.
Please clear my doubt.Am I right or wrong?

Damby
Guest
Damby

Bigup yourself Mr mkyong. You make my day

Alvaro Urbaez
Guest
Alvaro Urbaez

Thank you again, Mr mkyong.. hehe

Gabriel Ferreira
Guest
Gabriel Ferreira

um informação simples mais que muitos outro fórum não tem valeu ajudou muito.

Ahmed
Guest
Ahmed

very helpful

markus_w
Guest
markus_w

Thanks a lot. I had big issues with this topic. Greetings from Germany ;-)

yathiraju
Guest
yathiraju

thnk you . i learned lot of things from this website.

palmer24
Guest
palmer24

thank you

yathiraju
Guest
yathiraju

so nice information … thank you

Bilal
Guest
Bilal

Thank you for this. I still don’t fully understand redirect or what its uses are. But this is a great start.

Taake
Guest
Taake

This resolved a question I’ve had for about 4 years and nobody I knew had the answer :|

Funny thing is that you published this 3 years ago, Thanks a lot.

Arthur Grohe
Guest
Arthur Grohe

Thanks very much, Mkyong!
Your Explanations are always top-notch!

Joe C
Guest
Joe C

Perfect.. this solved an issue I was having with Rich:PanelMenu and templating… the URL would be one step behind the updated content.

gregtom
Guest
gregtom

Browser’s URL is not updated, but only for the first time. If the page1.xhtml makes a forward again, for example to a page2.xhtml, than the browser’s URL is updated and showes page1.xhtml. And so on. From this point the browser’s URL is always updated but it always shows the previous page. This is a totally unacceptable behavior.
So how the page forwarding navigation method can be used in a sense way?

Arthur Grohe
Guest
Arthur Grohe

Oh, you’re so right. This is absolut unacceptable. I think the only way to use forward the right way is with ajax, because then the browser never reloads….

Rajeshwari
Guest
Rajeshwari

Iam trying to redirect from servlet to xhtml page. But Iam getting “WELD-001303 No active contexts for scope type javax.enterprise.context.SessionScoped” Please help me to redirect.
Iam using Seam Framework and JSF and EJB’s in my Project

raccio
Guest
raccio

Hi
witch one to with a web application that requires authentication?
thanks

Abdullah
Guest
Abdullah

So, when shall we use page forward? I think it is a problem that the url is not updated.

Fernando Franzini
Guest
Fernando Franzini

Hi. You said that: “By default, JSF is perform a server page forward while navigating to another page.” But I did an example using servlet filter intercepting

	
		FiltrarRestricoes
		/*
		REQUEST
		FORWARD
		INCLUDE
		ERROR
	

The filter does not run when the navigation happens! You know why ?

Licnoln Baxter, III
Guest
Licnoln Baxter, III

This article is somewhat misleading.

The reason the Filter is not invoked is because the default JSF navigation does not actually perform a Servlet Forward; it merely renders the next page during processing of the same request. If you wish to use a Filter to intercept page transitions, then you should probably use the “?faces-redirect=true” parameter to forge a Redirecting navigation to occur.

trackback
JSF 2 does not update the url after a form action? » Mihail Stoynov's blog

[…] did not know that http://www.mkyong.com/jsf2/jsf-page-forward-vs-page-redirect/ Posted by mihail.stoynov at 14:02 Tagged with: java, jsf, jsf2, page […]

Van Long Nguyen
Guest
Van Long Nguyen

Thanks you very much. My problem solved!

tm
Guest
tm

“When is it necessary to use the page redirect?”

When we want to sent a link to someone or bookmark page. Then we must have actual URL. For example with “?id=2222”. This must be done with redirect.

joe
Guest
joe

When is it necessary to use the page redirect? The default page forward seems to work perfectly

Thanks