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

sendRedirect and forward

 
geet rahane
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q from 'Free SCWCD Mock exam for Servlets By Nikos'

What is the output of this code? (1 correct answer)

request.getRequestDispatcher("http://fcom.gr").forward(request, response);

1. The content of fcom.gr
2. HTTP Status 404

answer 2

# What is the output of this code? (1 correct answer)

response.sendRedirect("http://fcom.gr");

1. The content of fcom.gr
2. HTTP Status 404

answer 1

here when we give the argument as "http://fcom.gr" in both forward and sendRedirect...Is that means resource on another webapp...??? ........

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please quote your sources.
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be the same webapp or a different one. That depends on where the original request was sent i.e. where this code is running. The first one will not work because getRequestDispatcher method's argument must be in the same servlet context as given in the API

The pathname specified may be relative, although it cannot extend outside the current servlet context.


If the original request was indeed sent to http://fcom.gr, and if you want to dispatch the request to the welcome page, then you must use getRequestDispatcher("/") instead of using the domain name...
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys correct me if I'm wrong

geet rahane wrote:Q from 'Free SCWCD Mock exam for Servlets By Nikos'

What is the output of this code? (1 correct answer)

request.getRequestDispatcher("http://fcom.gr").forward(request, response);

1. The content of fcom.gr
2. HTTP Status 404

answer 2


forward is used to forward a request to a resource in the current application context.

geet rahane wrote:
# What is the output of this code? (1 correct answer)

response.sendRedirect("http://fcom.gr");

1. The content of fcom.gr
2. HTTP Status 404

answer 1

here when we give the argument as "http://fcom.gr" in both forward and sendRedirect...Is that means resource on another webapp...??? ........



sendRedirect will add the parameter given to sendRedirect as part of the response and make the browser dispatch a new request.

Hope that helps.

Kind Regards.
Hasnain.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic