aspose file tools*
The moose likes JSP and the fly likes jsp vs servlet request forwarding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp vs servlet request forwarding" Watch "jsp vs servlet request forwarding" New topic
Author

jsp vs servlet request forwarding

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 357
I came across following statement:


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.


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
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 :-)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
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.

Bill
 
 
subject: jsp vs servlet request forwarding