wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes forward vs include Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "forward vs include" Watch "forward vs include" New topic
Author

forward vs include

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
George Thomson
Ranch Hand

Joined: Jul 09, 2003
Posts: 107
the difference is while forwarding it commits the previous data, but while inlude it may not commit the previous data


George Thomson<br />SCJP, SCWCD, SCBCD, IBM-141(XML), SCEA<br />......................................................................
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
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...
Pravin Panicker
Ranch Hand

Joined: Oct 05, 2000
Posts: 62
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.


Pravin R Panicker<br />SCJP,SCWCD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 07, 2002
Posts: 298
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

Joined: Nov 22, 2008
Posts: 18944
what do you mean by paths must be adjusted?
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 07, 2002
Posts: 298
This is my understanding. I hope I am correct.
Yes, you are!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forward vs include