• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getRequestDispatcher

 
Ankur Jain Kothari
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why does the getRequestDispatcher of servletcontext allow only full path beginning with "/" ?
 
Chinmaya Chowdary
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Ted.

I think(I am guessing) this is due to support for foreign context and to increase the performance of the application.

In our servlet we get ServletContext object like,
Here the ServletContext object belongs to our current application.
Suppose we want to get resource from another application from within our application.
This can be done using foreign context like

Here the getContext("/WebApp2") method creates the another ServletContext object(which we refer as foreign context) and sets the path(/WebApp2) in HttpServletRequestImpl object(which will be implemented by container/server) like

and it also provide another method to get the context name like,


Now our request object is having foreign context name. Now we want to get the resource from another application from within our application. This can be done by using RequestDispatcher like,
Here the getRequestDispatcher() method creates the container/server implemented RequestDispatcher object, i.e. it implements the RequestDispatcher interface and it might provide another method to store the resource. Like it wraps the resource in it.
Suppose that method is

and it also provide another method to get the resource like,


Now we are going to forward the resource to the application 'WebApp2". This can be done by using,


Here the actual usage of using ("/") comes in. Before forwarding the resouce to the application(WebApp2), first it has know complete path of the resource. Here the path is '/WebApp2/result.jsp'. The forward() might implement like this,


Now we got the actual resource name (/WebApp2/result.jsp).

Suppose if the getRequestDispatcher() wont made compulsary of using ("/") before the resource. We will get the actual resource using


Here we used the string "/" to get the actual resource. We know the "/" is placed in string pool, whenever the class is loaded by the JVM. This will not be garbage collected, even when GC thread executed. It will stay as long as the class is present in JVM. It will occupy memory resources.

I think because of the above disadvantage, Sun made compulsary of using ("/") before resource.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic