• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

forward vs include

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In one of the discusiions in this forum they said
if forward is used the final url would be the called servlet URL and in case of include the final url would be calling servlets.
I tried an example and found that for both include and forward I see calling servlets uRL.
Eg
servlet A
doPost{ forward servlet B}
Final URL is of servlet A
servlet A
doPost{ include servlet B}
Final URL is of servlet A
Let me know if anyone has got a different answer.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the difference is while forwarding it commits the previous data, but while inlude it may not commit the previous data
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kylash Eeshwar,
Servlet specification 2.4 SRV 14.5.2.5 says that the response must be not committed when forwariding, and all data that was writen to the buffer must be cleared before forwarding. So, forwarding does not commit previous data...
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first case you still view the URL in your browser as A because the browser does not send a redirect request to the second url (B) as in case of response.sendRedirect.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the code I use to test forward and include . I am still not clear.
Please ecxplain.
doPost{}Servlet A
{
RequestDispatcher showDispatcher = req.getRequestDispatcher("fwdinclude");
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("url is " + req.getRequestURI());
showDispatcher.forward(req,resp);
//showDispatcher.include(req,resp);
}
final url after forwarding is http:localhost:9080/servlet/servletA
final url after including is http:localhost/servlet/servletA
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,
If you are usingn a named dispatcher (by calling SerlvetContext.getNamedDispatcher("some name")) and including the request, the behaviour must be:
none of the request attributes ("javax.servlet.include...")
must be set, and the paths (servletPath, requestURI...) remains as they were.
if you are forwarding to another resource
none of the request attributes ("javax.servlet.forward...")
must be set, and the paths (servletPath,requestURI...) must be ajusted
else
if you are not using a named dispatcher ( by calling ServletContext.getRequestDispatcher or ServletRequest.getRequestDispatcher) and including another resource:
the request attributes ("javax.servlet.include...") must be set,
and paths (servletPath,uri...,querystring) remains as they were
if you are forwarding to another resource
the request attributes ("javax.servlet.forward...") must be set,
must be set, and the paths (servletPath,requestURI...) must be ajusted

Notice that ServletRequest.getRequestURL() never changes
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you mean by paths must be adjusted?
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
request.getServletPath(),request.getPathInfo(),request.getQueryString(),request.getRequestURI() will always return the same value in a servlet and in an included/forwarded servlet?
Answer: no, these paths must be ajusted only to the forwarded servlet paths if this forwarded servlet had not been aquired with ServletContext.getNamedDispather() method!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my understanding. I hope I am correct.
ServletA
doPost{}{
RequestDispatcher showDispatcher = req.getRequestDispatcher("fwdinclude");
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
showDispatcher.forward(req,resp);
}
Final url on my browser is http://localhost:8080/servlet/A
ServletA
doPost{}{
RequestDispatcher showDispatcher = req.getRequestDispatcher("fwdinclude");
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
showDispatcher.include(req,resp);
}
Final url on my browser is http://localhost:8080/servlet/A
ServletA
doPost{}{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
res.sendRedirect("fwdinclude");
}
Final url on my browser is http://localhost:8080/servlet/fwdinclude
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my understanding. I hope I am correct.
Yes, you are!!
reply
    Bookmark Topic Watch Topic
  • New Topic