Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp vs servlet request forwarding

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Evans
Bartender
Posts: 1746
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13064
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic