Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use 'sendRedirect' and 'forward'

 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are 2 ways to call a page.
Way 1:

Way 2:

Can anybody explain the difference and usefulness of these 2 ways. Which situation is best suitable to which way.
I know the 'sendRedirect' needs the 'servletContext' also to be in its argument like "http://localhost:8080/MISApp/notloggedin.html" OR "/MISApp/notloggedin.html". Here 'MISApp' is the ServletContext Name, and the .forward wants the argument must start with a forward slash '/' which will be relative to the servlet context like

myServletConfig.getServletContext().getRequestDispatcher("/notloggedin.html").forward(req, res);

I don't want to hard-code the "http://***.com/***MyServletContext/" part anywhere in my code so that I can deploy with any servletContext. So I thought I can use the .forward tech all the times.
But the techniques are not working consistantly. Why? Sometimes if I put the sendRedirect only works. I want NOT to hard-code the "http://****.com/servletContext" part. Is this how you all write? Or is it ok to hard-code the above server and servletcontext info in the urls in our code? I am keeping the 'standard directory structure' of servlet API ver 2.2. I am using Tomcat and the servlet.jar which came with jswdk package,jdk1.2.2, on win 98.
Thanks for any info.
regds
maha anna

[This message has been edited by maha anna (edited October 03, 2000).]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sendRedirect is inefficient in that the redirect is sent to the client's browser which then requests the new page. So you are going back to the client and making the client request a page back on your server.
The RequestDispatcher is used to forward requests to another service (usually a servlet) on the same server. You should always use this method, if possible.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul. Sometimes when I put the equivalent of RequestDispatcher as sendRedirect only the the page is displayed. Also ,is this good idea to write the servletContext and the also in the urls? like "http://www.webappcabaret.com/maha/index.html" ? I try not to. Because sometimes we may be given different servletContext names.
regds
maha anna
 
Malu Sivasankar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Maha,
Instead of hard coding URLs we did the following in our project. We were using Apache Jserv.
In the servlet zone configuration file , global init parameters can be given. These global init parameters are given to each of the servlets and are accessible in the servlet via the method getInitParameter() in ServletConfig.

In the configuration file, init parameter was specified as follows
servlets.default.initArgs=host=127.0.0.1,zone=/leave/servlets/
In servlet,
String host = getInitParameter("host");
String zone = getInitParameter("zone");
and servlets were invoked as follows:-
out.println( "<FRAME NAME=\"LogoFrame\" src=\"http://"+ host + zone + "main.LogoServlet\">");
So during run time the url was resolved as
http://127.0.0.1/leave/servlets/main.LogoServlet
Similarly U may be able to configure Tomcat.
Regards,
Malu
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Malu. I really appreciate your tip.
regds
maha anna
 
vishal avad
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But .....
While Using Dispatcher I found that browser is not aware that which page has been called as it is forwared into server interally. So in case of addition / deletion or some specialised sort of events. Page will get resubmitted when user refresh / reload the page which is undesirable from user perspective.
In case when jsp is submitted to servlet and servlet calling same jsp then in that case there is really no way out.
If anybody ve some workaround to it.Please suggest
Thanks
Vishal Avad
 
Pravin Panicker
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Anna,
I guess this behaviour is because in case of forward, the request never leaves the container and remains within the same application context. So u need'nt specify /myApplication/redirectPage.jsp but just /redirectPage.jsp
In case of redirect though, the "new" request from client can be issued to "any" container. This explains why it needs the complete application context. So u say /myApplication/redirectPage.jsp
A work around would be append the redirect path with servletContext.getRealPath("/");
Correct me if i am wrong.
 
Ken Boyd
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sendRedirect(URL)- Browser has to get resource from give URL. So it might be same container or different web site or different application from same server
forward - has be from same container and application context as you use (/) in pointing your resource. so no absolute URL like http://www.xyz.com. You cannot directly forward or include a request to a resource in another application. If wish to do that first get servlet context of another application using this.getServletContext().getContext(uripath). Again remember uripath cannot be absoluate.

Hope this helps.
SCJP 1.4
SCWCD
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic