This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
JSP forward forwards the control to another resource available in the same web application on the same container, whereas Servlet forward forwards the control to another resource available in the same web application or different web app on the same container
If the above statement is right, then why is this so, since jsp is eventually 'translated' to servlet. Since at the end jsp and servlet both are same, why this difference.
With the <jsp:forward page="..."/> tag.
In Java code you use the requestDispatcher.forward(request, response) method.
With the JSP tag it uses the request Dispatcher for the current web application, so you can only forward within the same web application.
Via the servlet API you can obtain a request dispatcher for a context other than your current one and forward/include to that web application.
It normally requires some configuration to do this - e.g. Tomcat requires you to set the crossContext attribute for the context before it will give you a request dispatcher for another web app.
request.getRequestDispatcher behaves is used for the JSP tag.
Nothing prevents you from writing java code in the JSP to do this exact thing, but as you should know by now java code in a JSP is rather frowned upon :-)
Note that a JSP will have already written to the response when you forward it. Not necessarily the case when forwarding from a servlet.
This creates all sorts of opportunity for unexpected bugs and problems. If you think you really need to provide for forwarding from one jsp to another there is something wrong with your architecture - time to rethink.