Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forward() and sendRedirect()

 
ashok khetan
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
ashok.
 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashok

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
RD1.java

RD2.java


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 .
 
Win Yu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gagan Indus:
[B]Ashok
Save WEB-INF/web.xml as:


[/B]


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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic