jQuery in Action, 3rd edition
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes forward() and sendRedirect() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "forward() and sendRedirect()" Watch "forward() and sendRedirect()" New topic

forward() and sendRedirect()

ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
What do we mean by that 'call to forward() method of requestdispatcher is transparent to the browser'? I tried both forward() of rd and response.sendRedirect() , but the url in the browser doesn't get changed and it keeps the url of the servlet which contains these calls.
Gagan Indus
Ranch Hand

Joined: Feb 28, 2001
Posts: 346

May be you are missing something . The Browser's URL in case of response.sendRedirect() DOES get changed .

....call to forward() method of requestdispatcher is transparent to the browser...

When call to forward() of requestdispatcheris encountered , the request is transfered to another resource JSP/HTML/Servlet etc ( specified in requestDispatcher's constructor ) . The response generated by this new resource is what is sent back to the client
But response.sendRedirect() send back a new URL to client , which it should request . Client request for new URL and it is served by web-container . So client is invovled in all this
I guess above points make it clear , why forward() is transparent to client , while sendRedirect() is not

And to check this , you can use following setup
Make a directory 'rdapp' in your server's 'webapps' folder .
In WEB-INF/classes , place following two files


Save WEB-INF/web.xml as:

Make two html files in your 'rdapp' folder ( you choose their contents ) :
- rd1.html
- rd2.html

Now all is set , when you will access :
- http://localhost:8080/rdapp/servlet/RD1 , you will see contents of rd1.html , and the URL in browser will remain same
- http://localhost:8080/rdapp/servlet/RD2 , you will see contents of rd2.html , and the URL in browser will change to http://localhost:8080/rdapp/rd2.html
( i am using Resin server for all above setup ,should work fine with others too )
Hope that helps

Gagan (/^_^\) SCJP2
Die-hard JavaMonk -- little Java a day , keeps u going .

Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
Win Yu
Ranch Hand

Joined: Oct 17, 2001
Posts: 224
Originally posted by Gagan Indus:
Save WEB-INF/web.xml as:


Hi Gagan :
Your web.xml has problem.If you delete "<servlet-mapping url-pattern='/servlet/*' servlet-name='invoker'/>", it will works fine. Thanks for the nice example.

I agree. Here's the link: http://aspose.com/file-tools
subject: forward() and sendRedirect()
It's not a secret anymore!