• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

forward() and sendRedirect()

 
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.
 
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 .
 
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.
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic